1 /*
2 * Copyright (c) 2020-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 <stdlib.h>
16 #include "init_log.h"
17 #include "init_module_engine.h"
18 #include "loop_event.h"
19
20 static MODULE_MGR *defaultModuleMgr = NULL;
21 static MODULE_MGR *autorunModuleMgr = NULL;
22
InitModuleMgrInstall(const char * moduleName)23 int InitModuleMgrInstall(const char *moduleName)
24 {
25 if (moduleName == NULL) {
26 return -1;
27 }
28
29 if (defaultModuleMgr == NULL) {
30 defaultModuleMgr = ModuleMgrCreate("init");
31 }
32 if (defaultModuleMgr == NULL) {
33 return -1;
34 }
35
36 return ModuleMgrInstall(defaultModuleMgr, moduleName, 0, NULL);
37 }
38
InitModuleMgrUnInstall(const char * moduleName)39 void InitModuleMgrUnInstall(const char *moduleName)
40 {
41 ModuleMgrUninstall(defaultModuleMgr, moduleName);
42 }
43
DelayedUninstall(const IdleHandle taskHandle,void * context)44 static void DelayedUninstall(const IdleHandle taskHandle, void *context)
45 {
46 const char *moduleName = (const char *)context;
47 if (moduleName == NULL) {
48 return;
49 }
50 ModuleMgrUninstall(autorunModuleMgr, moduleName);
51 }
52
AutorunModuleMgrUnInstall(const char * moduleName)53 void AutorunModuleMgrUnInstall(const char *moduleName)
54 {
55 LE_DelayProc(LE_GetDefaultLoop(), DelayedUninstall, (void *)strdup(moduleName));
56 }
57
InitModuleDump(const MODULE_INFO * moduleInfo)58 static void InitModuleDump(const MODULE_INFO *moduleInfo)
59 {
60 printf("%s\n", moduleInfo->name);
61 }
62
InitModuleMgrDump(void)63 void InitModuleMgrDump(void)
64 {
65 if (defaultModuleMgr != NULL) {
66 ModuleMgrTraversal(defaultModuleMgr, NULL, InitModuleDump);
67 }
68
69 if (autorunModuleMgr != NULL) {
70 ModuleMgrTraversal(autorunModuleMgr, NULL, InitModuleDump);
71 }
72 }
73
ModuleMgrCmdInstall(int id,const char * name,int argc,const char ** argv)74 static int ModuleMgrCmdInstall(int id, const char *name, int argc, const char **argv)
75 {
76 INIT_ERROR_CHECK(argv != NULL && argc >= 1, return -1, "Invalid install parameter");
77 int ret;
78 if (defaultModuleMgr == NULL) {
79 defaultModuleMgr = ModuleMgrCreate("init");
80 }
81 ret = ModuleMgrInstall(defaultModuleMgr, argv[0], argc-1, argv+1);
82 INIT_ERROR_CHECK(ret == 0, return ret, "Install module %s fail errno %d", argv[0], ret);
83 return 0;
84 }
85
ModuleMgrCmdUninstall(int id,const char * name,int argc,const char ** argv)86 static int ModuleMgrCmdUninstall(int id, const char *name, int argc, const char **argv)
87 {
88 INIT_ERROR_CHECK(argv != NULL && argc >= 1, return -1, "Invalid install parameter");
89 ModuleMgrUninstall(defaultModuleMgr, argv[0]);
90 return 0;
91 }
92
moduleMgrCommandsInit(const HOOK_INFO * info,void * cookie)93 static int moduleMgrCommandsInit(const HOOK_INFO *info, void *cookie)
94 {
95 // "ohos.servicectrl.install"
96 (void)AddCmdExecutor("install", ModuleMgrCmdInstall);
97 (void)AddCmdExecutor("uninstall", ModuleMgrCmdUninstall);
98 // read cfg and start static plugin
99 return 0;
100 }
101
loadAutorunModules(const HOOK_INFO * info,void * cookie)102 static int loadAutorunModules(const HOOK_INFO *info, void *cookie)
103 {
104 autorunModuleMgr = ModuleMgrScan("init/autorun");
105 INIT_LOGV("Load autorun modules return autorunModuleMgr");
106 return 0;
107 }
108
MODULE_CONSTRUCTOR(void)109 MODULE_CONSTRUCTOR(void)
110 {
111 // Depends on parameter service
112 InitAddGlobalInitHook(0, loadAutorunModules);
113 InitAddPreCfgLoadHook(0, moduleMgrCommandsInit);
114 }
115