• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "init_plugin_manager.h"
16 
17 #include <dlfcn.h>
18 
19 #include "cJSON.h"
20 #include "init_param.h"
21 #include "init_utils.h"
22 #include "init_log.h"
23 #include "init_group_manager.h"
24 #include "init_plugin.h"
25 #include "init_service_manager.h"
26 #include "securec.h"
27 
28 #define MAX_CMD_ARGC 10
29 static int g_cmdExecutorId = 0;
30 static int g_cmdId = 0;
AddCmdExecutor(const char * cmdName,CmdExecutor execCmd)31 int AddCmdExecutor(const char *cmdName, CmdExecutor execCmd)
32 {
33     INIT_ERROR_CHECK(cmdName != NULL, return -1, "Invalid input param");
34     INIT_LOGI("AddCmdExecutor %s", cmdName);
35     PluginCmd *cmd = NULL;
36     InitGroupNode *groupNode = GetGroupNode(NODE_TYPE_CMDS, cmdName);
37     if (groupNode == NULL) {
38         groupNode = AddGroupNode(NODE_TYPE_CMDS, cmdName);
39         INIT_ERROR_CHECK(groupNode != NULL, return -1, "Failed to create group node");
40     }
41     cmd = groupNode->data.cmd;
42     if (cmd == NULL) {
43         cmd = (PluginCmd *)calloc(1, sizeof(PluginCmd));
44         INIT_ERROR_CHECK(cmd != NULL, return -1, "Failed to create cmd condition");
45         groupNode->data.cmd = cmd;
46         cmd->cmdId = g_cmdId++;
47         cmd->name = groupNode->name;
48         ListInit(&cmd->cmdExecutor);
49     }
50     if (execCmd == NULL) {
51         return 0;
52     }
53     PluginCmdExecutor *cmdExec = (PluginCmdExecutor *)calloc(1, sizeof(PluginCmdExecutor));
54     INIT_ERROR_CHECK(cmdExec != NULL, return -1, "Failed to create cmd listener");
55     ListInit(&cmdExec->node);
56     cmdExec->id = ++g_cmdExecutorId;
57     cmdExec->execCmd = execCmd;
58     ListAddTail(&cmd->cmdExecutor, &cmdExec->node);
59     return cmdExec->id;
60 }
61 
RemoveCmdExecutor(const char * cmdName,int id)62 static void RemoveCmdExecutor(const char *cmdName, int id)
63 {
64     INIT_ERROR_CHECK(cmdName != NULL, return, "Invalid input param");
65     InitGroupNode *groupNode = GetGroupNode(NODE_TYPE_CMDS, cmdName);
66     INIT_ERROR_CHECK(groupNode != NULL && groupNode->data.cmd != NULL,
67         return, "Can not find cmd %s", cmdName);
68 
69     PluginCmd *cmd = groupNode->data.cmd;
70     ListNode *node = cmd->cmdExecutor.next;
71     while (node != &cmd->cmdExecutor) {
72         PluginCmdExecutor *cmdExec = ListEntry(node, PluginCmdExecutor, node);
73         if (cmdExec->id == id) {
74             ListRemove(&cmdExec->node);
75             free(cmdExec);
76             break;
77         }
78         node = node->next;
79     }
80     if (cmd->cmdExecutor.next != &cmd->cmdExecutor) {
81         return;
82     }
83     DelGroupNode(NODE_TYPE_CMDS, cmdName);
84     free(cmd);
85 }
86 
PluginExecCmd_(PluginCmd * cmd,const char * cmdContent)87 void PluginExecCmd_(PluginCmd *cmd, const char *cmdContent)
88 {
89     const struct CmdArgs *ctx = GetCmdArg(cmdContent, " ", MAX_CMD_ARGC);
90     if (ctx == NULL) {
91         INIT_LOGE("Invalid arguments cmd: %s content: %s", cmd->name, cmdContent);
92         return;
93     } else if (ctx->argc > MAX_CMD_ARGC) {
94         INIT_LOGE("Invalid arguments cmd: %s content: %s argc: %d ",
95             cmd->name, cmdContent, ctx->argc);
96         FreeCmdArg((struct CmdArgs *)ctx);
97         return;
98     }
99     INIT_LOGV("PluginExecCmd_ index %s content: %s", cmd->name, cmdContent);
100     ListNode *node = cmd->cmdExecutor.next;
101     while (node != &cmd->cmdExecutor) {
102         PluginCmdExecutor *cmdExec = ListEntry(node, PluginCmdExecutor, node);
103         cmdExec->execCmd(cmdExec->id, cmd->name, ctx->argc, (const char **)ctx->argv);
104         node = node->next;
105     }
106     FreeCmdArg((struct CmdArgs *)ctx);
107 }
108 
PluginExecCmdByName(const char * name,const char * cmdContent)109 void PluginExecCmdByName(const char *name, const char *cmdContent)
110 {
111     INIT_ERROR_CHECK(name != NULL, return, "Invalid cmd for %s", cmdContent);
112     InitGroupNode *groupNode = GetGroupNode(NODE_TYPE_CMDS, name);
113     if (groupNode == NULL || groupNode->data.cmd == NULL) {
114         return;
115     }
116     PluginCmd *cmd = groupNode->data.cmd;
117     PluginExecCmd_(cmd, cmdContent);
118 }
119 
PluginExecCmd(const char * name,int argc,const char ** argv)120 int PluginExecCmd(const char *name, int argc, const char **argv)
121 {
122     INIT_ERROR_CHECK(name != NULL, return -1, "Invalid cmd ");
123     InitGroupNode *groupNode = GetGroupNode(NODE_TYPE_CMDS, name);
124     if (groupNode == NULL || groupNode->data.cmd == NULL) {
125         return -1;
126     }
127     PluginCmd *cmd = groupNode->data.cmd;
128     INIT_LOGI("PluginExecCmd index %s", cmd->name);
129     int ret = 0;
130     ListNode *node = cmd->cmdExecutor.next;
131     while (node != &cmd->cmdExecutor) {
132         PluginCmdExecutor *cmdExec = ListEntry(node, PluginCmdExecutor, node);
133         ret = cmdExec->execCmd(cmdExec->id, cmd->name, argc, argv);
134         node = node->next;
135     }
136     return ret;
137 }
138 
CompareCmdId(const HashNode * node,const void * key)139 static int CompareCmdId(const HashNode *node, const void *key)
140 {
141     InitGroupNode *groupNode = HASHMAP_ENTRY(node, InitGroupNode, hashNode);
142     if (groupNode == NULL || groupNode->data.cmd == NULL) {
143         return 1;
144     }
145     PluginCmd *cmd = groupNode->data.cmd;
146     return cmd->cmdId - *(int *)key;
147 }
148 
PluginExecCmdByCmdIndex(int index,const char * cmdContent)149 void PluginExecCmdByCmdIndex(int index, const char *cmdContent)
150 {
151     int hashCode = ((index >> 16) & 0x0000ffff) - 1; // 16 left shift
152     int cmdId = (index & 0x0000ffff);
153     HashNode *node = HashMapFind(GetGroupHashMap(NODE_TYPE_CMDS),
154         hashCode, (const void *)&cmdId, CompareCmdId);
155     if (node == NULL) {
156         return;
157     }
158     InitGroupNode *groupNode = HASHMAP_ENTRY(node, InitGroupNode, hashNode);
159     if (groupNode == NULL || groupNode->data.cmd == NULL) {
160         return;
161     }
162     PluginCmd *cmd = groupNode->data.cmd;
163     PluginExecCmd_(cmd, cmdContent);
164 }
165 
PluginGetCmdIndex(const char * cmdStr,int * index)166 const char *PluginGetCmdIndex(const char *cmdStr, int *index)
167 {
168     char cmdName[MAX_CMD_NAME_LEN] = {};
169     int i = 0;
170     while ((i < MAX_CMD_NAME_LEN) && (*(cmdStr + i) != '\0') && (*(cmdStr + i) != ' ')) {
171         cmdName[i] = *(cmdStr + i);
172         i++;
173     }
174     if (i >= MAX_CMD_NAME_LEN) {
175         return NULL;
176     }
177     cmdName[i] = '\0';
178     InitGroupNode *groupNode = GetGroupNode(NODE_TYPE_CMDS, cmdName);
179     if (groupNode == NULL || groupNode->data.cmd == NULL) {
180         AddCmdExecutor(cmdName, NULL);
181     }
182     groupNode = GetGroupNode(NODE_TYPE_CMDS, cmdName);
183     INIT_ERROR_CHECK(groupNode != NULL && groupNode->data.cmd != NULL,
184         return NULL, "Failed to create pluginCmd %s", cmdName);
185 
186     PluginCmd *cmd = groupNode->data.cmd;
187     int hashCode = GenerateHashCode(cmdName);
188     hashCode = (hashCode < 0) ? -hashCode : hashCode;
189     hashCode = hashCode % GROUP_HASHMAP_BUCKET;
190     *index = ((hashCode + 1) << 16) | cmd->cmdId; // 16 left shift
191     INIT_LOGI("PluginGetCmdIndex content: %s index %d", cmd->name, *index);
192     return cmd->name;
193 }
194 
LoadModule(const char * name,const char * libName)195 static int LoadModule(const char *name, const char *libName)
196 {
197     char path[128] = {0}; // 128 path for plugin
198     int ret = 0;
199     if (libName == NULL) {
200         ret = sprintf_s(path, sizeof(path), "%s/lib%s.z.so", DEFAULT_PLUGIN_PATH, name);
201     } else {
202         ret = sprintf_s(path, sizeof(path), "%s/%s", DEFAULT_PLUGIN_PATH, libName);
203     }
204     INIT_ERROR_CHECK(ret > 0, return -1, "Failed to sprintf path %s", name);
205     char *realPath = GetRealPath(path);
206     void* handle = dlopen(realPath, RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE);
207     if (handle == NULL) {
208         INIT_LOGE("Failed to load module %s, err %s", realPath, dlerror());
209         free(realPath);
210         return -1;
211     }
212     free(realPath);
213     dlclose(handle);
214     return 0;
215 }
216 
PluginInstall(const char * name,const char * libName)217 int PluginInstall(const char *name, const char *libName)
218 {
219     // load so, and init module
220     int ret = LoadModule(name, libName);
221     INIT_ERROR_CHECK(ret == 0, return -1, "pluginInit is null %s", name);
222 
223     PluginInfo *info = NULL;
224     InitGroupNode *groupNode = GetGroupNode(NODE_TYPE_PLUGINS, name);
225     if (groupNode != NULL && groupNode->data.pluginInfo != NULL) {
226         info = groupNode->data.pluginInfo;
227     }
228     INIT_ERROR_CHECK(info != NULL, return -1, "Failed to pluginInit %s", name);
229     INIT_LOGI("PluginInstall %s %d", name, info->state);
230     if (info->state == PLUGIN_STATE_INIT) {
231         INIT_ERROR_CHECK(info->pluginInit != NULL, return -1, "pluginInit is null %s", name);
232         ret = info->pluginInit();
233         INIT_ERROR_CHECK(ret == 0, return -1, "Failed to pluginInit %s", name);
234         info->state = PLUGIN_STATE_RUNNING;
235     }
236     return 0;
237 }
238 
PluginUninstall(const char * name)239 int PluginUninstall(const char *name)
240 {
241     InitGroupNode *groupNode = GetGroupNode(NODE_TYPE_PLUGINS, name);
242     INIT_ERROR_CHECK(groupNode != NULL && groupNode->data.pluginInfo != NULL,
243         return 0, "Can not find plugin %s", name);
244     PluginInfo *info = groupNode->data.pluginInfo;
245     INIT_ERROR_CHECK(info != NULL, return -1, "Failed to pluginInit %s", name);
246 
247     INIT_LOGI("PluginUninstall %s %d %p", name, info->state, info->pluginInit);
248     if (info->state == PLUGIN_STATE_RUNNING) {
249         INIT_ERROR_CHECK(info->pluginExit != NULL, return -1, "pluginExit is null %s", name);
250         info->pluginExit();
251         info->state = PLUGIN_STATE_INIT;
252     }
253     return 0;
254 }
255 
GetPluginInfo(const char * name)256 static PluginInfo *GetPluginInfo(const char *name)
257 {
258     InitGroupNode *groupNode = GetGroupNode(NODE_TYPE_PLUGINS, name);
259     if (groupNode == NULL) {
260         groupNode = AddGroupNode(NODE_TYPE_PLUGINS, name);
261         INIT_ERROR_CHECK(groupNode != NULL, return NULL, "Failed to create group node");
262     }
263     PluginInfo *info = groupNode->data.pluginInfo;
264     if (info == NULL) {
265         info = (PluginInfo *)calloc(1, sizeof(PluginInfo));
266         INIT_ERROR_CHECK(info != NULL, return NULL, "Failed to create module");
267         groupNode->data.pluginInfo = info;
268         info->name = groupNode->name;
269         info->state = 0;
270         info->startMode = 0;
271         info->libName = NULL;
272     }
273     return info;
274 }
275 
PluginRegister(const char * name,const char * config,int (* pluginInit)(void),void (* pluginExit)(void))276 static int PluginRegister(const char *name, const char *config, int (*pluginInit)(void), void (*pluginExit)(void))
277 {
278     INIT_LOGI("PluginRegister %s %p %p", name, pluginInit, pluginExit);
279     INIT_ERROR_CHECK(name != NULL, return -1, "Invalid plugin name");
280     INIT_ERROR_CHECK(pluginInit != NULL && pluginExit != NULL,
281         return -1, "Invalid plugin constructor %s", name);
282     InitServiceSpace();
283     PluginInfo *info = GetPluginInfo(name);
284     INIT_ERROR_CHECK(info != NULL, return -1, "Failed to create group node");
285     info->state = PLUGIN_STATE_INIT;
286     info->pluginInit = pluginInit;
287     info->pluginExit = pluginExit;
288 
289     // load config
290     if (config != NULL) {
291         ParseInitCfg(config, NULL);
292     }
293     return 0;
294 }
295 
PluginCmdInstall(int id,const char * name,int argc,const char ** argv)296 static int PluginCmdInstall(int id, const char *name, int argc, const char **argv)
297 {
298     INIT_ERROR_CHECK(argv != NULL && argc >= 1, return -1, "Invalid install parameter");
299     PluginInfo *info = GetPluginInfo(argv[0]);
300     int ret = 0;
301     if (info == NULL) {
302         ret = PluginInstall(argv[0], NULL);
303     } else {
304         ret = PluginInstall(argv[0], info->libName);
305     }
306     INIT_ERROR_CHECK(ret == 0, return ret, "Install plugin %s fail", argv[0]);
307     return 0;
308 }
309 
PluginCmdUninstall(int id,const char * name,int argc,const char ** argv)310 static int PluginCmdUninstall(int id, const char *name, int argc, const char **argv)
311 {
312     INIT_ERROR_CHECK(argv != NULL && argc >= 1, return -1, "Invalid install parameter");
313     int ret = PluginUninstall(argv[0]);
314     INIT_ERROR_CHECK(ret == 0, return ret, "Uninstall plugin %s fail", argv[0]);
315     return 0;
316 }
317 
LoadPluginCfg(void)318 static int LoadPluginCfg(void)
319 {
320     char *fileBuf = ReadFileToBuf(DEFAULT_PLUGIN_CFG);
321     INIT_ERROR_CHECK(fileBuf != NULL, return -1, "Failed to read file content %s", DEFAULT_PLUGIN_CFG);
322     cJSON *root = cJSON_Parse(fileBuf);
323     INIT_ERROR_CHECK(root != NULL, free(fileBuf);
324         return -1, "Failed to parse json file %s", DEFAULT_PLUGIN_CFG);
325     int itemNumber = 0;
326     cJSON *json = GetArrayItem(root, &itemNumber, "modules");
327     if (json == NULL) {
328         free(fileBuf);
329         return 0;
330     }
331     for (int i = 0; i < itemNumber; ++i) {
332         cJSON *item = cJSON_GetArrayItem(json, i);
333         char *moduleName = cJSON_GetStringValue(cJSON_GetObjectItem(item, "name"));
334         if (moduleName == NULL) {
335             INIT_LOGE("Failed to get plugin info");
336             continue;
337         }
338         PluginInfo *info = GetPluginInfo(moduleName);
339         if (info == NULL) {
340             INIT_LOGE("Failed to get plugin for module %s", moduleName);
341             continue;
342         }
343 
344         info->startMode = 0;
345         char *mode = cJSON_GetStringValue(cJSON_GetObjectItem(item, "start-mode"));
346         if (mode == NULL || (strcmp(mode, "static") != 0)) {
347             info->startMode = 1;
348         }
349         char *libName = cJSON_GetStringValue(cJSON_GetObjectItem(item, "lib-name"));
350         if (libName != NULL) {
351             info->libName = strdup(libName); // do not care error
352         }
353         INIT_LOGI("LoadPluginCfg module %s %d libName %s", moduleName, info->startMode, info->libName);
354         if (info->startMode == 0) {
355             PluginInstall(moduleName, info->libName);
356         }
357     }
358     free(fileBuf);
359     return 0;
360 }
361 
PluginManagerInit(void)362 void PluginManagerInit(void)
363 {
364     // "ohos.servicectrl.install"
365     (void)AddCmdExecutor("install", PluginCmdInstall);
366     (void)AddCmdExecutor("uninstall", PluginCmdUninstall);
367     // register interface
368     SetPluginInterface();
369     // read cfg and start static plugin
370     LoadPluginCfg();
371 }
372 
SetPluginInterface(void)373 int SetPluginInterface(void)
374 {
375     static PluginInterface *pluginInterface = NULL;
376     if (pluginInterface != NULL) {
377         return 0;
378     }
379     char *realPath = GetRealPath("/system/lib/libplugin.z.so");
380 #ifndef STARTUP_INIT_TEST
381     void* handle = dlopen(realPath, RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE);
382     if (handle == NULL) {
383         INIT_LOGE("Failed to load module %s, err %s", realPath, dlerror());
384         free(realPath);
385         return -1;
386     }
387     GetPluginInterfaceFunc getPluginInterface = (GetPluginInterfaceFunc)dlsym(handle, "GetPluginInterface");
388 #else
389     GetPluginInterfaceFunc getPluginInterface = GetPluginInterface;
390 #endif
391     INIT_LOGI("PluginManagerInit getPluginInterface %p ", getPluginInterface);
392     if (getPluginInterface != NULL) {
393         pluginInterface = getPluginInterface();
394         if (pluginInterface != NULL) {
395             pluginInterface->pluginRegister = PluginRegister;
396             pluginInterface->addCmdExecutor = AddCmdExecutor;
397             pluginInterface->removeCmdExecutor = RemoveCmdExecutor;
398             pluginInterface->systemWriteParam = SystemWriteParam;
399             pluginInterface->systemReadParam = SystemReadParam;
400             pluginInterface->securityLabelSet = NULL;
401         }
402         INIT_LOGI("PluginManagerInit pluginInterface %p %p %p",
403             pluginInterface, pluginInterface->pluginRegister, getPluginInterface);
404     }
405     free(realPath);
406 #ifndef STARTUP_INIT_TEST
407     dlclose(handle);
408 #endif
409     return 0;
410 }
411