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