modulemgr.h 4.3 KB
Newer Older
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 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 118 119 120 121 122 123 124 125 126 127 128 129 130
/*
 * 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.
 */

#ifndef OHOS_MODULES_MANAGER_H__
#define OHOS_MODULES_MANAGER_H__

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif

/**
 * @brief Module constructor function
 *
 * For static modules, this code is executed before main().
 * For dynamic modules, this code is executed when ModuleMgrInstall().
 *
 * Usage example:
 *   MODULE_CONSTRUCTOR(void)
 *   {
 *       ...
 *   }
 */
#define MODULE_CONSTRUCTOR(void) static void _init(void) __attribute__((constructor)); static void _init(void)

/**
 * @brief Module destructor function
 *
 * For static modules, this code will not be executed.
 * For dynamic modules, this code is executed when ModuleMgrUninstall().
 *
 * Usage example:
 *   MODULE_DESTRUCTOR(void)
 *   {
 *       ...
 *   }
 */
#define MODULE_DESTRUCTOR(void) static void _destroy(void) __attribute__((destructor)); static void _destroy(void)

// Forward declaration
typedef struct tagMODULE_MGR MODULE_MGR;

/**
 * @brief Create dynamic module manager
 *
 * This dynamic module manager will manager modules
 * in the directory: /system/lib/{name}/
 * @param name module manager name
 * @return return module manager handle if succeed; return NULL if failed.
 */
MODULE_MGR *ModuleMgrCreate(const char *name);

/**
 * @brief Destroy dynamic module manager
 *
 * It will uninstall all modules managed by this moduleMgr
 * @param moduleMgr module manager handle
 * @return None
 */
void ModuleMgrDestroy(MODULE_MGR *moduleMgr);

/**
 * @brief Install a module
 * 
 * The final module path is: /system/lib/{moduleMgrPath}/{moduleNmae}.z.so
 *
 * @param moduleMgr module manager handle
 * @param moduleName module name
 * @param argc argument counts for installing
 * @param argv arguments for installing, the last argument is NULL.
 * @return module handle returned by dlopen
 */
int ModuleMgrInstall(MODULE_MGR *moduleMgr, const char *moduleName,
                     int argc, const char *argv[]);

/**
 * @brief Module install arguments
 */
typedef struct {
    int argc;
    const char **argv;
} MODULE_INSTALL_ARGS;

/**
 * @brief Get module install arguments
 * 
 * This function is available only in MODULE_CONSTRUCTOR.
 *
 * @return install args if succeed; return NULL if failed.
 */
const MODULE_INSTALL_ARGS *ModuleMgrGetArgs(void);

/**
 * @brief Scan and install all modules in specified directory
 *
 * @param modulePath path for modules to be installed
 * @return install args if succeed; return NULL if failed.
 */
MODULE_MGR *ModuleMgrScan(const char *modulePath);

/**
 * @brief Uninstall module
 *
 * @param moduleMgr module manager handle
 * @param name module name. If name is NULL, it will uninstall all modules.
 * @return install args if succeed; return NULL if failed.
 */
void ModuleMgrUninstall(MODULE_MGR *moduleMgr, const char *name);

/**
 * @brief Get number of modules in module manager
 *
 * @param hookMgr module manager handle
 * @return number of modules, return 0 if none
 */
int ModuleMgrGetCnt(const MODULE_MGR *moduleMgr);

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
/**
 * @brief Module information for traversing modules
 */
typedef struct tagMODULE_INFO {
    const char *name;     /* module name */
    void *handle;         /* module handler */
    void *cookie;         /* hook execution cookie */
} MODULE_INFO;

/**
 * @brief Module traversal function prototype
 *
 * @param moduleInfo MODULE_INFO for traversing each module.
 * @return None
 */
typedef void (*OhosModuleTraversal)(const MODULE_INFO *moduleInfo);

/**
 * @brief Traversing all modules in the ModuleManager
 *
 * @param moduleMgr module manager handle
 * @param cookie traversal cookie.
 * @param traversal traversal function.
 * @return None.
 */
void ModuleMgrTraversal(const MODULE_MGR *moduleMgr, void *cookie, OhosModuleTraversal traversal);

158 159 160 161 162 163
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif
#endif