init_group_manager.c 10.8 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",
X
xionglei6 已提交
38
        "device.charing.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);
X
xionglei6 已提交
69 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    char *fileBuf = ReadFileToBuf(groupCfg);
    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
            HashMapRemove(g_initWorkspace.hashMap[groupNode->type], groupNode->name);
        }
        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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
        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);
}

static void GroupNodeFree(const HashNode *node)
{
    InitGroupNode *groupNode = HASHMAP_ENTRY(node, InitGroupNode, hashNode);
    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++) {
        int ret = HashMapCreate(&g_initWorkspace.hashMap[i], &info);
        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;
    }
    // get boot mode
    char *data = ReadFileData(BOOT_CMD_LINE);
    if (data != NULL) {
        int ret = GetProcCmdlineValue(BOOT_GROUP_NAME, data,
            g_initWorkspace.groupModeStr, sizeof(g_initWorkspace.groupModeStr));
        if (ret != 0) {
            INIT_LOGE("%s", "Failed to get boot group");
X
xionglei6 已提交
191 192 193 194 195 196 197
#ifdef INIT_TEST
            if (GetBootModeFromMisc() == GROUP_CHARING) {
                strcpy_s(g_initWorkspace.groupModeStr, sizeof(g_initWorkspace.groupModeStr), "device.charing.group");
            } else {
                strcpy_s(g_initWorkspace.groupModeStr, sizeof(g_initWorkspace.groupModeStr), BOOT_GROUP_DEFAULT);
            }
#else
X
xionglei6 已提交
198
            strcpy_s(g_initWorkspace.groupModeStr, sizeof(g_initWorkspace.groupModeStr), BOOT_GROUP_DEFAULT);
X
xionglei6 已提交
199
#endif
X
xionglei6 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212
        }
        free(data);
    }
    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 已提交
213 214
    INIT_ERROR_CHECK(realPath != NULL, return -1,
        "Failed to get path for %s", g_initWorkspace.groupModeStr);
X
xionglei6 已提交
215 216 217
    InitParseGroupCfg_(realPath);
    InitGroupNode *groupRoot = g_initWorkspace.groupNodes[NODE_TYPE_GROUPS];
    int level = 0;
X
xionglei6 已提交
218
    while ((groupRoot != NULL) && (level < GROUP_IMPORT_MAX_LEVEL)) { // for more import
X
xionglei6 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
        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;
    }
    INIT_LOGV("AddGroupNode type %d %p name %s", type, g_initWorkspace.hashMap[type], name);
    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
        HashMapAdd(g_initWorkspace.hashMap[type], &groupNode->hashNode);
    }
    return groupNode;
}

InitGroupNode *GetGroupNode(int type, const char *name)
{
    if (type >= NODE_TYPE_GROUPS) {
        return NULL;
    }
    INIT_LOGV("GetGroupNode type %d %p name %s", type, g_initWorkspace.hashMap[type], name);
    HashNode *node = HashMapGet(g_initWorkspace.hashMap[type], name);
    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;
    }
    INIT_LOGV("DelGroupNode type %d %p name %s", type, g_initWorkspace.hashMap[type], name);
    HashMapRemove(g_initWorkspace.hashMap[type], name);
    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;
    }
X
xionglei6 已提交
306
#ifndef INIT_TEST
X
xionglei6 已提交
307 308 309 310 311 312 313
    if (g_initWorkspace.groupMode == GROUP_BOOT) {
        return 0;
    }
    HashNode *node = HashMapGet(g_initWorkspace.hashMap[type], name);
    if (node != NULL) {
        return 0;
    }
X
xionglei6 已提交
314 315 316 317 318 319 320 321 322 323
#else
    HashNode *node = HashMapGet(g_initWorkspace.hashMap[type], name);
    if (node != NULL) {
        INIT_LOGI("Found %s in %s group", name, type == NODE_TYPE_JOBS ? "job" : "service");
        return 0;
    }
    if (g_initWorkspace.groupMode == GROUP_BOOT) {
        return 0;
    }
#endif
X
xionglei6 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
    return -1;
}

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

#ifdef STARTUP_INIT_TEST
InitWorkspace *GetInitWorkspace(void)
{
    return &g_initWorkspace;
}
#endif