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