modulemgr.c 8.7 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 84 85 86 87
    free((void *)moduleMgr);
}

/*
 * Module Item related api
 */


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

static void moduleDestroy(ListNode *node)
{
    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 98 99 100 101 102 103 104 105 106
    free((void *)module);
}

static MODULE_INSTALL_ARGS *currentInstallArgs = NULL;

static void *moduleInstall(MODULE_ITEM *module, int argc, const char *argv[])
{
    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
    }
M
Mupceet 已提交
118
    BEGET_LOGV("moduleInstall path %s", path);
119 120 121
    currentInstallArgs = &(module->moduleMgr->installArgs);
    handle = dlopen(path, RTLD_LAZY | RTLD_GLOBAL);
    currentInstallArgs = NULL;
L
laiguizhong 已提交
122
    BEGET_CHECK_ONLY_ELOG(handle != NULL, "moduleInstall path %s fail %d", path, errno);
123 124 125 126 127 128 129 130 131 132
    return handle;
}

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

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

    module->name = strdup(moduleName);
    if (module->name == NULL) {
        moduleDestroy((ListNode *)module);
        return -1;
    }

    // Install
    module->handle = moduleInstall(module, argc, argv);
    if (module->handle == NULL) {
M
Mupceet 已提交
152
        BEGET_LOGE("Failed to install module %s", moduleName);
153 154 155 156 157
        moduleDestroy((ListNode *)module);
        return -1;
    }

    // Add to list
158
    OH_ListAddTail(&(moduleMgr->modules), (ListNode *)module);
159 160 161 162 163 164 165 166 167 168 169 170 171 172

    return 0;
}

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

static int stringEndsWith(const char *srcStr, const char *endStr)
{
    int srcStrLen = strlen(srcStr);
    int endStrLen = strlen(endStr);

L
laiguizhong 已提交
173
    BEGET_CHECK(!(srcStrLen < endStrLen), return -1);
174 175

    srcStr += (srcStrLen - endStrLen);
L
laiguizhong 已提交
176
    BEGET_CHECK(strcmp(srcStr, endStr) != 0, return (srcStrLen - endStrLen));
177 178 179 180 181 182 183 184 185 186 187
    return -1;
}

static void scanModules(MODULE_MGR *moduleMgr, const char *path)
{
    int end;
    int ret;
    DIR *dir;
    struct dirent *file;

    dir = opendir(path);
L
laiguizhong 已提交
188
    BEGET_CHECK(dir != NULL, return);
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

    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
        end = stringEndsWith(file->d_name, MODULE_SUFFIX_D);
        if (end <= 0) {
            continue;
        }

        file->d_name[end] = '\0';
M
Mupceet 已提交
206 207 208 209 210 211
        BEGET_LOGV("scanModules module %s", file->d_name);
        if (strncmp(file->d_name, "lib", strlen("lib")) == 0) {
            ret = ModuleMgrInstall(moduleMgr, file->d_name + strlen("lib"), 0, NULL);
        } else {
            ret = ModuleMgrInstall(moduleMgr, file->d_name, 0, NULL);
        }
212 213 214 215 216 217 218 219 220 221 222 223 224 225
    }

    closedir(dir);
}

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

    moduleMgr = ModuleMgrCreate(modulePath);
L
laiguizhong 已提交
226
    BEGET_CHECK(moduleMgr != NULL, return NULL);
227 228

    if (modulePath[0] == '/') {
229 230 231 232 233
        scanModules(moduleMgr, modulePath);
    } else if (InUpdaterMode() == 1) {
        BEGET_CHECK(snprintf_s(path, sizeof(path), sizeof(path) - 1,
            "/%s/%s", MODULE_LIB_NAME, modulePath) > 0, return NULL);
        scanModules(moduleMgr, path);
234
    } else {
235 236 237 238 239 240 241 242 243
        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]) {
                scanModules(moduleMgr, files->paths[i]);
            }
        }
        FreeCfgFiles(files);
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    }
    return moduleMgr;
}

static int moduleCompare(ListNode *node, void *data)
{
    MODULE_ITEM *module = (MODULE_ITEM *)node;

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

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

    // Remove from the list
273
    OH_ListRemove((ListNode *)module);
274 275 276 277 278 279
    // Destroy the module
    moduleDestroy((ListNode *)module);
}

int ModuleMgrGetCnt(const MODULE_MGR *moduleMgr)
{
L
laiguizhong 已提交
280
    BEGET_CHECK(moduleMgr != NULL, return 0);
281
    return OH_ListGetCnt(&(moduleMgr->modules));
282
}
283 284 285 286 287 288 289 290 291 292 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

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

static int moduleTraversalProc(ListNode *node, void *cookie)
{
    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;
324
    OH_ListTraversal((ListNode *)(&(moduleMgr->modules)), (void *)(&args), moduleTraversalProc, 0);
325
}