init_group_manager.c 11.4 KB
Newer Older
X
xionglei6 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "init_group_manager.h"

#include "init_jobs_internal.h"
#include "init_log.h"
#include "init_utils.h"
#include "securec.h"
#include "init_service_manager.h"

static InitWorkspace g_initWorkspace = {0, 0, {0}, {0}, {0}};
int GenerateHashCode(const char *key)
{
    int code = 0;
X
xionglei6 已提交
27 28
    size_t keyLen = strlen(key);
    for (size_t i = 0; i < keyLen; i++) {
X
xionglei6 已提交
29 30 31 32 33 34 35 36 37
        code += key[i] - 'A';
    }
    return code;
}

static int GetBootGroupMode(void)
{
    static const char *groupModes[] = {
        "device.boot.group",
M
Mupceet 已提交
38
        "device.charge.group"
X
xionglei6 已提交
39 40 41 42 43 44
    };
    for (size_t i = 0; i < ARRAY_LENGTH(groupModes); i++) {
        if (strcmp(g_initWorkspace.groupModeStr, groupModes[i]) == 0) {
            return i;
        }
    }
X
xionglei6 已提交
45
    return (int)GROUP_UNKNOW;
X
xionglei6 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
}

static int ParseGroupCfgItem(cJSON *root, int type, const char *itemName)
{
    int itemNumber = 0;
    cJSON *json = GetArrayItem(root, &itemNumber, itemName);
    if (json == NULL) {
        return 0;
    }

    for (int i = 0; i < itemNumber; ++i) {
        cJSON *item = cJSON_GetArrayItem(json, i);
        char *strValue = cJSON_GetStringValue(item);
        if (strValue != NULL) {
            AddGroupNode(type, strValue);
        }
    }
    return 0;
}

static int InitParseGroupCfg_(const char *groupCfg)
{
X
xionglei6 已提交
68
    INIT_LOGI("Parse group config %s", groupCfg);
M
Mupceet 已提交
69
    char *fileBuf = ReadFileData(groupCfg);
X
xionglei6 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    INIT_ERROR_CHECK(fileBuf != NULL, return -1, "Failed to read file content %s", groupCfg);
    cJSON *fileRoot = cJSON_Parse(fileBuf);
    INIT_ERROR_CHECK(fileRoot != NULL, free(fileBuf);
        return -1, "Failed to parse json file %s", groupCfg);

    ParseGroupCfgItem(fileRoot, NODE_TYPE_JOBS, "jobs");
    ParseGroupCfgItem(fileRoot, NODE_TYPE_SERVICES, "services");
    ParseGroupCfgItem(fileRoot, NODE_TYPE_GROUPS, "groups");
    cJSON_Delete(fileRoot);
    free(fileBuf);
    return 0;
}

static int InitImportGroupCfg_(InitGroupNode *groupRoot)
{
    InitGroupNode *groupNode = groupRoot;
    while (groupNode != NULL) {
        groupRoot = groupNode->next;
        InitParseGroupCfg_(groupNode->name);
        free(groupNode);
        groupNode = groupRoot;
    }
    return 0;
}

static int InitFreeGroupNodes_(InitGroupNode *groupRoot)
{
    InitGroupNode *groupNode = groupRoot;
    while (groupNode != NULL) {
        groupRoot = groupNode->next;
        if (groupNode->type < NODE_TYPE_GROUPS) { // remove from hashmap
101
            OH_HashMapRemove(g_initWorkspace.hashMap[groupNode->type], groupNode->name);
X
xionglei6 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
        }
        free(groupNode);
        groupNode = groupRoot;
    }
    return 0;
}

static char *GetAbsolutePath(const char *path, const char *cfgName, char *buffer, uint32_t buffSize)
{
    int len = 0;
    size_t cfgNameLen = strlen(cfgName);
    int ext = 0;
    if (cfgNameLen > strlen(".cfg")) {
        ext = strcmp(cfgName + cfgNameLen - strlen(".cfg"), ".cfg") == 0;
    }
    if (cfgName[0] != '/') {
X
xionglei6 已提交
118
        const char *format = ((ext != 0) ? "%s/%s" : "%s/%s.cfg");
X
xionglei6 已提交
119 120
        len = sprintf_s(buffer, buffSize, format, path, cfgName);
    } else {
X
xionglei6 已提交
121
        const char *format = ((ext != 0) ? "%s" : "%s.cfg");
X
xionglei6 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
        len = sprintf_s(buffer, buffSize, format, cfgName);
    }
    if (len <= 0) {
        return NULL;
    }
    buffer[len] = '\0';
    return buffer;
}

static int GroupNodeNodeCompare(const HashNode *node1, const HashNode *node2)
{
    InitGroupNode *groupNode1 = HASHMAP_ENTRY(node1, InitGroupNode, hashNode);
    InitGroupNode *groupNode2 = HASHMAP_ENTRY(node2, InitGroupNode, hashNode);
    return strcmp(groupNode1->name, groupNode2->name);
}

static int GroupNodeKeyCompare(const HashNode *node1, const void *key)
{
    InitGroupNode *groupNode1 = HASHMAP_ENTRY(node1, InitGroupNode, hashNode);
    return strcmp(groupNode1->name, (char *)key);
}

static int GroupNodeGetKeyHashCode(const void *key)
{
    return GenerateHashCode((const char *)key);
}

static int GroupNodeGetNodeHashCode(const HashNode *node)
{
    InitGroupNode *groupNode = HASHMAP_ENTRY(node, InitGroupNode, hashNode);
    return GenerateHashCode((const char *)groupNode->name);
}

C
cheng_jinsong 已提交
155
static void GroupNodeFree(const HashNode *node, void *context)
X
xionglei6 已提交
156 157
{
    InitGroupNode *groupNode = HASHMAP_ENTRY(node, InitGroupNode, hashNode);
C
cheng_jinsong 已提交
158 159 160 161 162 163 164
    if (groupNode->type == NODE_TYPE_SERVICES) {
        ReleaseService(groupNode->data.service);
        groupNode->data.service = NULL;
    } else if (groupNode->type == NODE_TYPE_CMDS) {
        ReleaseCmd(groupNode->data.cmd);
        groupNode->data.cmd = NULL;
    }
X
xionglei6 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    free(groupNode);
}

void InitServiceSpace(void)
{
    if (g_initWorkspace.initFlags != 0) {
        return;
    }
    HashInfo info = {
        GroupNodeNodeCompare,
        GroupNodeKeyCompare,
        GroupNodeGetNodeHashCode,
        GroupNodeGetKeyHashCode,
        GroupNodeFree,
        GROUP_HASHMAP_BUCKET
    };
    for (size_t i = 0; i < ARRAY_LENGTH(g_initWorkspace.hashMap); i++) {
182
        int ret = OH_HashMapCreate(&g_initWorkspace.hashMap[i], &info);
X
xionglei6 已提交
183 184 185 186 187 188 189 190
        if (ret != 0) {
            INIT_LOGE("%s", "Failed to create hash map");
        }
    }

    for (int i = 0; i < NODE_TYPE_MAX; i++) {
        g_initWorkspace.groupNodes[i] = NULL;
    }
M
Mupceet 已提交
191 192
    // get boot mode, set default mode
    strcpy_s(g_initWorkspace.groupModeStr, sizeof(g_initWorkspace.groupModeStr), BOOT_GROUP_DEFAULT);
C
cheng_jinsong 已提交
193 194 195 196 197 198
    int ret = GetParameterFromCmdLine(BOOT_GROUP_NAME,
        g_initWorkspace.groupModeStr, sizeof(g_initWorkspace.groupModeStr));
    if (ret != 0) {
        INIT_LOGV("Failed to get boot group");
        if (GetBootModeFromMisc() == GROUP_CHARGE) {
            strcpy_s(g_initWorkspace.groupModeStr, sizeof(g_initWorkspace.groupModeStr), "device.charge.group");
X
xionglei6 已提交
199 200 201 202 203 204 205 206 207 208 209 210
        }
    }
    INIT_LOGI("boot start %s", g_initWorkspace.groupModeStr);
    g_initWorkspace.groupMode = GetBootGroupMode();
    g_initWorkspace.initFlags = 1;
}

int InitParseGroupCfg(void)
{
    char buffer[128] = {0}; // 128 buffer size
    char *realPath = GetAbsolutePath(GROUP_DEFAULT_PATH,
        g_initWorkspace.groupModeStr, buffer, sizeof(buffer));
X
xionglei6 已提交
211 212
    INIT_ERROR_CHECK(realPath != NULL, return -1,
        "Failed to get path for %s", g_initWorkspace.groupModeStr);
X
xionglei6 已提交
213 214 215
    InitParseGroupCfg_(realPath);
    InitGroupNode *groupRoot = g_initWorkspace.groupNodes[NODE_TYPE_GROUPS];
    int level = 0;
X
xionglei6 已提交
216
    while ((groupRoot != NULL) && (level < GROUP_IMPORT_MAX_LEVEL)) { // for more import
X
xionglei6 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
        g_initWorkspace.groupNodes[NODE_TYPE_GROUPS] = NULL;
        InitImportGroupCfg_(groupRoot);
        groupRoot = g_initWorkspace.groupNodes[NODE_TYPE_GROUPS];
        level++;
    }
    InitFreeGroupNodes_(g_initWorkspace.groupNodes[NODE_TYPE_GROUPS]);
    g_initWorkspace.groupNodes[NODE_TYPE_GROUPS] = NULL;
    return 0;
}

InitGroupNode *AddGroupNode(int type, const char *name)
{
    INIT_ERROR_CHECK(type <= NODE_TYPE_MAX, return NULL, "Invalid type");
    INIT_ERROR_CHECK(name != NULL, return NULL, "Invalid name");
    InitGroupNode *groupNode = GetGroupNode(type, name);
    if (groupNode != NULL) {
        return groupNode;
    }
C
chengjinsong2 已提交
235
    INIT_LOGV("AddGroupNode type %d name %s", type, name);
X
xionglei6 已提交
236 237 238 239 240 241 242 243 244 245 246
    uint32_t nameLen = (uint32_t)strlen(name);
    groupNode = (InitGroupNode *)calloc(1, sizeof(InitGroupNode) + nameLen + 1);
    INIT_ERROR_CHECK(groupNode != NULL, return NULL, "Failed to alloc for group %s", name);
    int ret = memcpy_s(groupNode->name, nameLen + 1, name, nameLen + 1);
    INIT_ERROR_CHECK(ret == 0, free(groupNode);
        return NULL, "Failed to alloc for group %s", name);
    groupNode->type = type;
    groupNode->next = g_initWorkspace.groupNodes[type];
    g_initWorkspace.groupNodes[type] = groupNode;

    if (type < NODE_TYPE_GROUPS) { // add hashmap
247
        OH_HashMapAdd(g_initWorkspace.hashMap[type], &groupNode->hashNode);
X
xionglei6 已提交
248 249 250 251 252 253 254 255 256
    }
    return groupNode;
}

InitGroupNode *GetGroupNode(int type, const char *name)
{
    if (type >= NODE_TYPE_GROUPS) {
        return NULL;
    }
257
    HashNode *node = OH_HashMapGet(g_initWorkspace.hashMap[type], name);
X
xionglei6 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
    if (node == NULL) {
        return NULL;
    }
    return HASHMAP_ENTRY(node, InitGroupNode, hashNode);
}

InitGroupNode *GetNextGroupNode(int type, const InitGroupNode *curr)
{
    INIT_ERROR_CHECK(type <= NODE_TYPE_MAX, return NULL, "Invalid type");
    if (curr == NULL) {
        return g_initWorkspace.groupNodes[type];
    }
    return curr->next;
}

void DelGroupNode(int type, const char *name)
{
    if (type >= NODE_TYPE_GROUPS) {
        return;
    }
C
chengjinsong2 已提交
278
    INIT_LOGV("DelGroupNode type %d name %s", type, name);
279
    OH_HashMapRemove(g_initWorkspace.hashMap[type], name);
X
xionglei6 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
    InitGroupNode *groupNode = g_initWorkspace.groupNodes[type];
    InitGroupNode *preNode = groupNode;
    while (groupNode != NULL) {
        if (strcmp(groupNode->name, name) != 0) {
            preNode = groupNode;
            groupNode = groupNode->next;
            continue;
        }
        if (groupNode == g_initWorkspace.groupNodes[type]) {
            g_initWorkspace.groupNodes[type] = groupNode->next;
        } else {
            preNode->next = groupNode->next;
        }
        free(groupNode);
        break;
    }
}

int CheckNodeValid(int type, const char *name)
{
    if (type >= NODE_TYPE_GROUPS) {
        return -1;
    }
303
    HashNode *node = OH_HashMapGet(g_initWorkspace.hashMap[type], name);
X
xionglei6 已提交
304
    if (node != NULL) {
C
cheng_jinsong 已提交
305
        INIT_LOGV("Found %s in %s group", name, type == NODE_TYPE_JOBS ? "job" : "service");
X
xionglei6 已提交
306 307 308
        return 0;
    }
    if (g_initWorkspace.groupMode == GROUP_BOOT) {
M
Mupceet 已提交
309 310 311 312
        // for boot start, can not start charger service
        if (strcmp(name, "charger") == 0) {
            return -1;
        }
X
xionglei6 已提交
313 314
        return 0;
    }
X
xionglei6 已提交
315 316 317 318 319 320 321 322 323 324 325
    return -1;
}

HashMapHandle GetGroupHashMap(int type)
{
    if (type >= NODE_TYPE_GROUPS) {
        return NULL;
    }
    return g_initWorkspace.hashMap[type];
}

C
cheng_jinsong 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
void CloseServiceSpace(void)
{
    if (g_initWorkspace.initFlags == 0) {
        return;
    }
    for (size_t i = 0; i < ARRAY_LENGTH(g_initWorkspace.hashMap); i++) {
        if (g_initWorkspace.hashMap[i] != NULL) {
            HashMapHandle handle = g_initWorkspace.hashMap[i];
            g_initWorkspace.hashMap[i] = NULL;
            OH_HashMapDestory(handle, NULL);
        }
    }
    g_initWorkspace.initFlags = 0;
}

void ReleaseCmd(PluginCmd *cmd)
{
    if (cmd == NULL) {
        return;
    }
    ListNode *node = cmd->cmdExecutor.next;
    while (node != &cmd->cmdExecutor) {
        PluginCmdExecutor *cmdExec = ListEntry(node, PluginCmdExecutor, node);
        OH_ListRemove(&cmdExec->node);
        free(cmdExec);
        node = cmd->cmdExecutor.next;
    }
    free(cmd);
}

X
xionglei6 已提交
356 357 358 359 360 361
#ifdef STARTUP_INIT_TEST
InitWorkspace *GetInitWorkspace(void)
{
    return &g_initWorkspace;
}
#endif