1d9f0492fSopenharmony_ci/*
2d9f0492fSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3d9f0492fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4d9f0492fSopenharmony_ci * you may not use this file except in compliance with the License.
5d9f0492fSopenharmony_ci * You may obtain a copy of the License at
6d9f0492fSopenharmony_ci *
7d9f0492fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8d9f0492fSopenharmony_ci *
9d9f0492fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10d9f0492fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11d9f0492fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12d9f0492fSopenharmony_ci * See the License for the specific language governing permissions and
13d9f0492fSopenharmony_ci * limitations under the License.
14d9f0492fSopenharmony_ci */
15d9f0492fSopenharmony_ci#include "init_context.h"
16d9f0492fSopenharmony_ci
17d9f0492fSopenharmony_ci#include "init_module_engine.h"
18d9f0492fSopenharmony_ci#include "init_utils.h"
19d9f0492fSopenharmony_ci#include "plugin_adapter.h"
20d9f0492fSopenharmony_ci#include "securec.h"
21d9f0492fSopenharmony_ci
22d9f0492fSopenharmony_cistatic SubInitContext g_subInitContext[INIT_CONTEXT_MAIN] = { 0 };
23d9f0492fSopenharmony_cistatic ConfigContext g_currContext = { INIT_CONTEXT_MAIN };
24d9f0492fSopenharmony_cistatic int g_subInitRunning = 0;
25d9f0492fSopenharmony_ci
26d9f0492fSopenharmony_ciint InitSubInitContext(InitContextType type, const SubInitContext *context)
27d9f0492fSopenharmony_ci{
28d9f0492fSopenharmony_ci    PLUGIN_CHECK(type < INIT_CONTEXT_MAIN, return -1, "Invalid type %d", type);
29d9f0492fSopenharmony_ci    PLUGIN_CHECK(context != NULL, return -1, "Invalid context %d", type);
30d9f0492fSopenharmony_ci    g_subInitContext[type].type = type;
31d9f0492fSopenharmony_ci    g_subInitContext[type].startSubInit = context->startSubInit;
32d9f0492fSopenharmony_ci    g_subInitContext[type].stopSubInit = context->stopSubInit;
33d9f0492fSopenharmony_ci    g_subInitContext[type].executeCmdInSubInit = context->executeCmdInSubInit;
34d9f0492fSopenharmony_ci    g_subInitContext[type].setSubInitContext = context->setSubInitContext;
35d9f0492fSopenharmony_ci    return 0;
36d9f0492fSopenharmony_ci}
37d9f0492fSopenharmony_ci
38d9f0492fSopenharmony_civoid StopSubInit(pid_t pid)
39d9f0492fSopenharmony_ci{
40d9f0492fSopenharmony_ci    if (g_subInitContext[0].stopSubInit != NULL) {
41d9f0492fSopenharmony_ci        g_subInitContext[0].stopSubInit(pid);
42d9f0492fSopenharmony_ci    }
43d9f0492fSopenharmony_ci}
44d9f0492fSopenharmony_ci
45d9f0492fSopenharmony_ciint StartSubInit(InitContextType type)
46d9f0492fSopenharmony_ci{
47d9f0492fSopenharmony_ci    PLUGIN_CHECK(type < INIT_CONTEXT_MAIN, return -1, "Invalid type %d", type);
48d9f0492fSopenharmony_ci    if (!g_subInitRunning) {
49d9f0492fSopenharmony_ci        // install init context
50d9f0492fSopenharmony_ci#ifndef STARTUP_INIT_TEST
51d9f0492fSopenharmony_ci        InitModuleMgrInstall("init_context");
52d9f0492fSopenharmony_ci#endif
53d9f0492fSopenharmony_ci    }
54d9f0492fSopenharmony_ci    PLUGIN_CHECK(g_subInitContext[type].startSubInit != NULL, return -1, "Invalid context %d", type);
55d9f0492fSopenharmony_ci    g_subInitRunning = 1;
56d9f0492fSopenharmony_ci    return g_subInitContext[type].startSubInit(type);
57d9f0492fSopenharmony_ci}
58d9f0492fSopenharmony_ci
59d9f0492fSopenharmony_ciint ExecuteCmdInSubInit(const ConfigContext *context, const char *name, const char *cmdContent)
60d9f0492fSopenharmony_ci{
61d9f0492fSopenharmony_ci    PLUGIN_CHECK(context != NULL, return -1, "Invalid context");
62d9f0492fSopenharmony_ci    PLUGIN_CHECK(name != NULL, return -1, "Invalid name");
63d9f0492fSopenharmony_ci    PLUGIN_CHECK(context->type < INIT_CONTEXT_MAIN, return -1, "Invalid type %d", context->type);
64d9f0492fSopenharmony_ci    PLUGIN_LOGV("Execute command '%s %s' in context %d", name, cmdContent, context->type);
65d9f0492fSopenharmony_ci    StartSubInit(context->type);
66d9f0492fSopenharmony_ci    PLUGIN_CHECK(g_subInitContext[context->type].executeCmdInSubInit != NULL,
67d9f0492fSopenharmony_ci        return -1, "Invalid context %d", context->type);
68d9f0492fSopenharmony_ci    return g_subInitContext[context->type].executeCmdInSubInit(context->type, name, cmdContent);
69d9f0492fSopenharmony_ci}
70d9f0492fSopenharmony_ci
71d9f0492fSopenharmony_ciint SetSubInitContext(const ConfigContext *context, const char *service)
72d9f0492fSopenharmony_ci{
73d9f0492fSopenharmony_ci    if (InUpdaterMode() == 1) { // not support sub init in update mode
74d9f0492fSopenharmony_ci        return 0;
75d9f0492fSopenharmony_ci    }
76d9f0492fSopenharmony_ci    PLUGIN_CHECK(context != NULL, return -1, "Invalid context");
77d9f0492fSopenharmony_ci    if (context->type >= INIT_CONTEXT_MAIN) {
78d9f0492fSopenharmony_ci        g_currContext.type = INIT_CONTEXT_MAIN;
79d9f0492fSopenharmony_ci        return 0;
80d9f0492fSopenharmony_ci    }
81d9f0492fSopenharmony_ci    g_currContext.type = context->type;
82d9f0492fSopenharmony_ci    PLUGIN_CHECK(g_subInitContext[context->type].setSubInitContext != NULL,
83d9f0492fSopenharmony_ci        return -1, "Invalid context %d", context->type);
84d9f0492fSopenharmony_ci    PLUGIN_LOGI("Set selinux context %d for %s", context->type, service);
85d9f0492fSopenharmony_ci    return g_subInitContext[context->type].setSubInitContext(context->type);
86d9f0492fSopenharmony_ci}
87d9f0492fSopenharmony_ci
88d9f0492fSopenharmony_ciint CheckExecuteInSubInit(const ConfigContext *context)
89d9f0492fSopenharmony_ci{
90d9f0492fSopenharmony_ci    if (InUpdaterMode() == 1) { // not support sub init in update mode
91d9f0492fSopenharmony_ci        return 0;
92d9f0492fSopenharmony_ci    }
93d9f0492fSopenharmony_ci#ifdef INIT_SUPPORT_CHIPSET_INIT
94d9f0492fSopenharmony_ci    return !(context == NULL || context->type == INIT_CONTEXT_MAIN || g_currContext.type != INIT_CONTEXT_MAIN);
95d9f0492fSopenharmony_ci#else
96d9f0492fSopenharmony_ci    return 0;
97d9f0492fSopenharmony_ci#endif
98d9f0492fSopenharmony_ci}
99d9f0492fSopenharmony_ci
100d9f0492fSopenharmony_ci#ifdef STARTUP_INIT_TEST
101d9f0492fSopenharmony_ciSubInitContext *GetSubInitContext(InitContextType type)
102d9f0492fSopenharmony_ci{
103d9f0492fSopenharmony_ci    PLUGIN_CHECK(type < INIT_CONTEXT_MAIN, return NULL, "Invalid type %d", type);
104d9f0492fSopenharmony_ci    return &g_subInitContext[type];
105d9f0492fSopenharmony_ci}
106d9f0492fSopenharmony_ci#endif
107