modulemgr.c 8.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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.
 */

M
Mupceet 已提交
16
#include <dirent.h>
17 18 19 20 21 22
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <linux/limits.h>

M
Mupceet 已提交
23
#include "beget_ext.h"
24
#include "config_policy_utils.h"
M
Mupceet 已提交
25
#include "init_utils.h"
26 27 28 29 30
#include "list.h"
#include "securec.h"
#include "modulemgr.h"

#define MODULE_SUFFIX_D ".z.so"
31 32 33 34 35
#ifdef SUPPORT_64BIT
#define MODULE_LIB_NAME "lib64"
#else
#define MODULE_LIB_NAME "lib"
#endif
36 37 38 39 40 41 42 43 44 45 46

struct tagMODULE_MGR {
    ListNode modules;
    const char *name;
    MODULE_INSTALL_ARGS installArgs;
};

MODULE_MGR *ModuleMgrCreate(const char *name)
{
    MODULE_MGR *moduleMgr;

L
laiguizhong 已提交
47
    BEGET_CHECK(name != NULL, return NULL);
48 49

    moduleMgr = (MODULE_MGR *)malloc(sizeof(MODULE_MGR));
L
laiguizhong 已提交
50
    BEGET_CHECK(moduleMgr != NULL, return NULL);
51
    OH_ListInit(&(moduleMgr->modules));
52 53 54 55 56 57 58 59 60 61 62 63 64
    moduleMgr->name = strdup(name);
    if (moduleMgr->name == NULL) {
        free((void *)moduleMgr);
        return NULL;
    }
    moduleMgr->installArgs.argc = 0;
    moduleMgr->installArgs.argv = NULL;

    return moduleMgr;
}

void ModuleMgrDestroy(MODULE_MGR *moduleMgr)
{
L
laiguizhong 已提交
65
    BEGET_CHECK(moduleMgr != NULL, return);
66 67

    ModuleMgrUninstall(moduleMgr, NULL);
L
laiguizhong 已提交
68
    BEGET_CHECK(moduleMgr->name == NULL, free((void *)moduleMgr->name));
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    free((void *)moduleMgr);
}

/*
 * Module Item related api
 */


typedef struct tagMODULE_ITEM {
    ListNode node;
    MODULE_MGR *moduleMgr;
    const char *name;
    void *handle;
} MODULE_ITEM;

C
cheng_jinsong 已提交
84
static void ModuleDestroy(ListNode *node)
85 86 87
{
    MODULE_ITEM *module;

L
laiguizhong 已提交
88
    BEGET_CHECK(node != NULL, return);
89 90

    module = (MODULE_ITEM *)node;
L
laiguizhong 已提交
91 92
    BEGET_CHECK(module->name == NULL, free((void *)module->name));
    BEGET_CHECK(module->handle == NULL, dlclose(module->handle));
93 94 95 96 97
    free((void *)module);
}

static MODULE_INSTALL_ARGS *currentInstallArgs = NULL;

C
cheng_jinsong 已提交
98
static void *ModuleInstall(MODULE_ITEM *module, int argc, const char *argv[])
99 100 101 102 103 104 105 106
{
    void *handle;
    char path[PATH_MAX];

    module->moduleMgr->installArgs.argc = argc;
    module->moduleMgr->installArgs.argv = argv;

    if (module->moduleMgr->name[0] == '/') {
M
Mupceet 已提交
107 108 109 110
        if (snprintf_s(path, sizeof(path), sizeof(path) - 1, "%s/%s" MODULE_SUFFIX_D,
            module->moduleMgr->name, module->name) < 0) {
            return NULL;
        }
111
    } else {
M
Mupceet 已提交
112 113 114
        const char *fmt = (InUpdaterMode() == 0) ? "/system/" MODULE_LIB_NAME : "/" MODULE_LIB_NAME;
        if (snprintf_s(path, sizeof(path), sizeof(path) - 1,
            "%s/%s/lib%s" MODULE_SUFFIX_D, fmt, module->moduleMgr->name, module->name) < 0) {
M
Mupceet 已提交
115 116
            return NULL;
        }
117
    }
C
cheng_jinsong 已提交
118
    BEGET_LOGV("Module install path %s", path);
119 120 121
    currentInstallArgs = &(module->moduleMgr->installArgs);
    handle = dlopen(path, RTLD_LAZY | RTLD_GLOBAL);
    currentInstallArgs = NULL;
C
cheng_jinsong 已提交
122
    BEGET_CHECK_ONLY_ELOG(handle != NULL, "ModuleInstall path %s fail %d", path, errno);
123 124 125
    return handle;
}

C
cheng_jinsong 已提交
126 127 128 129 130 131 132
static int ModuleCompare(ListNode *node, void *data)
{
    MODULE_ITEM *module = (MODULE_ITEM *)node;

    return strcmp(module->name, (char *)data);
}

133 134 135 136 137 138 139
/*
 * 用于扫描安装指定目录下所有的插件。
 */
int ModuleMgrInstall(MODULE_MGR *moduleMgr, const char *moduleName,
                     int argc, const char *argv[])
{
    MODULE_ITEM *module;
M
Mupceet 已提交
140
    BEGET_LOGV("ModuleMgrInstall moduleName %s", moduleName);
141
    // Get module manager
L
laiguizhong 已提交
142
    BEGET_CHECK(!(moduleMgr == NULL || moduleName == NULL), return -1);
C
cheng_jinsong 已提交
143 144 145 146

    module = (MODULE_ITEM *)OH_ListFind(&(moduleMgr->modules), (void *)moduleName, ModuleCompare);
    BEGET_ERROR_CHECK(module == NULL, return 0, "%s module already exists", moduleName);

147 148
    // Create module item
    module = (MODULE_ITEM *)malloc(sizeof(MODULE_ITEM));
L
laiguizhong 已提交
149
    BEGET_CHECK(module != NULL, return -1);
150 151 152 153 154 155

    module->handle = NULL;
    module->moduleMgr = moduleMgr;

    module->name = strdup(moduleName);
    if (module->name == NULL) {
C
cheng_jinsong 已提交
156
        ModuleDestroy((ListNode *)module);
157 158 159 160
        return -1;
    }

    // Install
C
cheng_jinsong 已提交
161
    module->handle = ModuleInstall(module, argc, argv);
162
    if (module->handle == NULL) {
M
Mupceet 已提交
163
        BEGET_LOGE("Failed to install module %s", moduleName);
C
cheng_jinsong 已提交
164
        ModuleDestroy((ListNode *)module);
165 166 167 168
        return -1;
    }

    // Add to list
169
    OH_ListAddTail(&(moduleMgr->modules), (ListNode *)module);
170 171 172 173 174 175 176 177 178

    return 0;
}

const MODULE_INSTALL_ARGS *ModuleMgrGetArgs(void)
{
    return currentInstallArgs;
}

C
cheng_jinsong 已提交
179
static int StringEndsWith(const char *srcStr, const char *endStr)
180 181 182 183
{
    int srcStrLen = strlen(srcStr);
    int endStrLen = strlen(endStr);

L
laiguizhong 已提交
184
    BEGET_CHECK(!(srcStrLen < endStrLen), return -1);
185 186

    srcStr += (srcStrLen - endStrLen);
L
laiguizhong 已提交
187
    BEGET_CHECK(strcmp(srcStr, endStr) != 0, return (srcStrLen - endStrLen));
188 189 190
    return -1;
}

C
cheng_jinsong 已提交
191
static void ScanModules(MODULE_MGR *moduleMgr, const char *path)
192 193 194 195 196 197
{
    int end;
    DIR *dir;
    struct dirent *file;

    dir = opendir(path);
L
laiguizhong 已提交
198
    BEGET_CHECK(dir != NULL, return);
199 200 201 202 203 204 205 206 207 208 209

    while (1) {
        file = readdir(dir);
        if (file == NULL) {
            break;
        }
        if ((file->d_type != DT_REG) && (file->d_type != DT_LNK)) {
            continue;
        }

        // Must be ended with MODULE_SUFFIX_D
C
cheng_jinsong 已提交
210
        end = StringEndsWith(file->d_name, MODULE_SUFFIX_D);
211 212 213 214 215
        if (end <= 0) {
            continue;
        }

        file->d_name[end] = '\0';
C
cheng_jinsong 已提交
216
        BEGET_LOGV("Scan module with name %s", file->d_name);
M
Mupceet 已提交
217
        if (strncmp(file->d_name, "lib", strlen("lib")) == 0) {
C
cheng_jinsong 已提交
218
            ModuleMgrInstall(moduleMgr, file->d_name + strlen("lib"), 0, NULL);
M
Mupceet 已提交
219
        } else {
C
cheng_jinsong 已提交
220
            ModuleMgrInstall(moduleMgr, file->d_name, 0, NULL);
M
Mupceet 已提交
221
        }
222 223 224 225 226 227 228 229 230 231 232 233 234 235
    }

    closedir(dir);
}

/*
 * 用于扫描安装指定目录下所有的插件。
 */
MODULE_MGR *ModuleMgrScan(const char *modulePath)
{
    MODULE_MGR *moduleMgr;
    char path[PATH_MAX];

    moduleMgr = ModuleMgrCreate(modulePath);
L
laiguizhong 已提交
236
    BEGET_CHECK(moduleMgr != NULL, return NULL);
237 238

    if (modulePath[0] == '/') {
C
cheng_jinsong 已提交
239
        ScanModules(moduleMgr, modulePath);
240 241 242
    } else if (InUpdaterMode() == 1) {
        BEGET_CHECK(snprintf_s(path, sizeof(path), sizeof(path) - 1,
            "/%s/%s", MODULE_LIB_NAME, modulePath) > 0, return NULL);
C
cheng_jinsong 已提交
243
        ScanModules(moduleMgr, path);
244
    } else {
245 246 247 248 249
        BEGET_CHECK(snprintf_s(path, sizeof(path), sizeof(path) - 1,
            "%s/%s", MODULE_LIB_NAME, modulePath) > 0, return NULL);
        CfgFiles *files = GetCfgFiles(path);
        for (int i = MAX_CFG_POLICY_DIRS_CNT - 1; files && i >= 0; i--) {
            if (files->paths[i]) {
C
cheng_jinsong 已提交
250
                ScanModules(moduleMgr, files->paths[i]);
251 252 253
            }
        }
        FreeCfgFiles(files);
254 255 256 257 258 259 260 261 262 263
    }
    return moduleMgr;
}

/*
 * 卸载指定插件。
 */
void ModuleMgrUninstall(MODULE_MGR *moduleMgr, const char *name)
{
    MODULE_ITEM *module;
L
laiguizhong 已提交
264
    BEGET_CHECK(moduleMgr != NULL, return);
265 266
    // Uninstall all modules if no name specified
    if (name == NULL) {
C
cheng_jinsong 已提交
267
        OH_ListRemoveAll(&(moduleMgr->modules), ModuleDestroy);
268 269
        return;
    }
M
Mupceet 已提交
270
    BEGET_LOGV("ModuleMgrUninstall moduleName %s", name);
271
    // Find module by name
C
cheng_jinsong 已提交
272
    module = (MODULE_ITEM *)OH_ListFind(&(moduleMgr->modules), (void *)name, ModuleCompare);
M
Mupceet 已提交
273
    BEGET_ERROR_CHECK(module != NULL, return, "Can not find module %s", name);
274 275

    // Remove from the list
276
    OH_ListRemove((ListNode *)module);
277
    // Destroy the module
C
cheng_jinsong 已提交
278
    ModuleDestroy((ListNode *)module);
279 280 281 282
}

int ModuleMgrGetCnt(const MODULE_MGR *moduleMgr)
{
L
laiguizhong 已提交
283
    BEGET_CHECK(moduleMgr != NULL, return 0);
284
    return OH_ListGetCnt(&(moduleMgr->modules));
285
}
286 287 288 289 290 291

typedef struct tagMODULE_TRAVERSAL_ARGS {
    void  *cookie;
    OhosModuleTraversal traversal;
} MODULE_TRAVERSAL_ARGS;

C
cheng_jinsong 已提交
292
static int ModuleTraversalProc(ListNode *node, void *cookie)
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
{
    MODULE_ITEM *module;
    MODULE_TRAVERSAL_ARGS *args;
    MODULE_INFO info;

    module = (MODULE_ITEM *)node;
    args = (MODULE_TRAVERSAL_ARGS *)cookie;

    info.cookie = args->cookie;
    info.handle = module->handle;
    info.name = module->name;
    args->traversal(&info);

    return 0;
}

/**
 * @brief Traversing all hooks in the HookManager
 *
 * @param moduleMgr HookManager handle.
 *                If hookMgr is NULL, it will use default HookManager
 * @param cookie traversal cookie.
 * @param traversal traversal function.
 * @return None.
 */
void ModuleMgrTraversal(const MODULE_MGR *moduleMgr, void *cookie, OhosModuleTraversal traversal)
{
    MODULE_TRAVERSAL_ARGS args;
    if (moduleMgr == NULL) {
        return;
    }

    args.cookie = cookie;
    args.traversal = traversal;
C
cheng_jinsong 已提交
327
    OH_ListTraversal((ListNode *)(&(moduleMgr->modules)), (void *)(&args), ModuleTraversalProc, 0);
328
}