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 "script_instructionhelper.h"
16 #include <dlfcn.h>
17 #include <set>
18 #include "scope_guard.h"
19 #include "script_basicinstruction.h"
20 #include "script_loadscript.h"
21 #include "script_manager_impl.h"
22 #include "script_registercmd.h"
23 #include "script_updateprocesser.h"
24 #include "script_utils.h"
25
26 using namespace BasicInstruction;
27 using namespace Updater;
28
29 namespace Uscript {
30 static std::set<std::string> g_reservedInstructions = {
31 "LoadScript", "RegisterCmd", "abort", "assert", "concat",
32 "is_substring", "stdout", "sleep", "set_progress", "ui_print",
33 "show_progress"
34 };
35
36 static ScriptInstructionHelper* g_instructionHelper = nullptr;
37
GetBasicInstructionHelper(ScriptManagerImpl * impl)38 ScriptInstructionHelper* ScriptInstructionHelper::GetBasicInstructionHelper(ScriptManagerImpl *impl)
39 {
40 if (g_instructionHelper == nullptr) {
41 if (impl == nullptr) {
42 return nullptr;
43 }
44 g_instructionHelper = new ScriptInstructionHelper(impl);
45 }
46 return g_instructionHelper;
47 }
48
ReleaseBasicInstructionHelper()49 void ScriptInstructionHelper::ReleaseBasicInstructionHelper()
50 {
51 if (g_instructionHelper != nullptr) {
52 delete g_instructionHelper;
53 }
54 g_instructionHelper = nullptr;
55 }
56
~ScriptInstructionHelper()57 ScriptInstructionHelper::~ScriptInstructionHelper()
58 {
59 if (instrLib_ != nullptr) {
60 dlclose(instrLib_);
61 }
62 instrLib_ = nullptr;
63 }
64
RegisterInstructions() const65 int32_t ScriptInstructionHelper::RegisterInstructions() const
66 {
67 scriptManager_->AddInstruction("RegisterCmder", new ScriptRegisterCmd());
68 scriptManager_->AddInstruction("LoadScript", new ScriptLoadScript());
69 scriptManager_->AddInstruction("Stdout", new UScriptInstructionStdout());
70 scriptManager_->AddInstruction("Abort", new UScriptInstructionAbort());
71 scriptManager_->AddInstruction("Assert", new UScriptInstructionAssert());
72 scriptManager_->AddInstruction("Sleep", new UScriptInstructionSleep());
73 scriptManager_->AddInstruction("Concat", new UScriptInstructionConcat());
74 scriptManager_->AddInstruction("IsSubString", new UScriptInstructionIsSubString());
75 scriptManager_->AddInstruction("set_progress", new UScriptInstructionSetProcess());
76 scriptManager_->AddInstruction("show_progress", new UScriptInstructionShowProcess());
77 scriptManager_->AddInstruction("ui_print", new UScriptInstructionUiPrint());
78 return USCRIPT_SUCCESS;
79 }
80
IsReservedInstruction(const std::string & scriptName) const81 bool ScriptInstructionHelper::IsReservedInstruction(const std::string &scriptName) const
82 {
83 if (g_reservedInstructions.find(scriptName) != g_reservedInstructions.end()) {
84 return true;
85 }
86 return false;
87 }
88
AddScript(const std::string & scriptName,int32_t priority) const89 int32_t ScriptInstructionHelper::AddScript(const std::string &scriptName, int32_t priority) const
90 {
91 return scriptManager_->AddScript(scriptName, priority);
92 }
93
AddInstruction(const std::string & instrName,const UScriptInstructionPtr instr)94 int32_t ScriptInstructionHelper::AddInstruction(const std::string &instrName, const UScriptInstructionPtr instr)
95 {
96 if (IsReservedInstruction(instrName)) {
97 USCRIPT_LOGE(" %s reserved", instrName.c_str());
98 return USCRIPT_ERROR_REVERED;
99 }
100 return scriptManager_->AddInstruction(instrName, instr);
101 }
102
RegisterUserInstruction(const std::string & libName,const std::string & instrName)103 int32_t ScriptInstructionHelper::RegisterUserInstruction(const std::string& libName,
104 const std::string &instrName)
105 {
106 // first get realpath of libName, then compare with realLibName
107 char *realPath = realpath(libName.c_str(), nullptr);
108 USCRIPT_CHECK(realPath != nullptr, return USCRIPT_INVALID_PARAM, "realPath is NULL %s", libName.c_str());
109 std::string realLibName = realPath;
110 free(realPath);
111 if (!userInstrLibName_.empty() && userInstrLibName_.compare(realLibName) != 0) {
112 USCRIPT_LOGE("Lib name must be equal %s ", realLibName.c_str());
113 return USCRIPT_INVALID_PARAM;
114 }
115
116 userInstrLibName_.assign(realLibName);
117 Uscript::UScriptInstructionFactoryPtr factory = nullptr;
118 if (instrLib_ == nullptr) {
119 instrLib_ = dlopen(realLibName.c_str(), RTLD_LAZY | RTLD_LOCAL);
120 }
121 USCRIPT_CHECK(instrLib_ != nullptr, return USCRIPT_INVALID_PARAM,
122 "Fail to dlopen %s ", libName.c_str());
123 auto pGetInstructionFactory = (Uscript::UScriptInstructionFactoryPtr(*)())dlsym(instrLib_, "GetInstructionFactory");
124 auto pReleaseInstructionFactory =
125 (void(*)(Uscript::UScriptInstructionFactoryPtr))dlsym(instrLib_, "ReleaseInstructionFactory");
126 if (pReleaseInstructionFactory == nullptr || pGetInstructionFactory == nullptr) {
127 USCRIPT_LOGE("Fail to get sym %s", libName.c_str());
128 return USCRIPT_INVALID_PARAM;
129 }
130 factory = pGetInstructionFactory();
131 USCRIPT_CHECK(factory != nullptr, return USCRIPT_INVALID_PARAM,
132 "Fail to create instruction factory for %s", instrName.c_str());
133 ON_SCOPE_EXIT(freeFactory) {
134 pReleaseInstructionFactory(factory);
135 };
136 // Create instruction and register it
137 UScriptInstructionPtr instr = nullptr;
138 int32_t ret = factory->CreateInstructionInstance(instr, instrName);
139 if (ret != USCRIPT_SUCCESS || instr == nullptr) {
140 USCRIPT_LOGE("Fail to create instruction for %s", instrName.c_str());
141 return ret == USCRIPT_SUCCESS ? USCRIPT_ERROR_CREATE_OBJ : USCRIPT_NOTEXIST_INSTRUCTION;
142 }
143
144 ret = AddInstruction(instrName, instr);
145 if (ret != USCRIPT_SUCCESS) {
146 USCRIPT_LOGE("Fail to add instruction for %s", instrName.c_str());
147 // ret is USCRIPT_ERROR_REVERED, instr register failed, can be deleted
148 delete instr;
149 instr = nullptr;
150 }
151 // ScriptManagerImpl::AddInstruction has saved instr, don't delete it here!!!
152 return ret;
153 }
154
RegisterUserInstruction(const std::string & instrName,Uscript::UScriptInstructionFactory * factory)155 int32_t ScriptInstructionHelper::RegisterUserInstruction(const std::string &instrName,
156 Uscript::UScriptInstructionFactory *factory)
157 {
158 if (factory == nullptr) {
159 USCRIPT_LOGE("%s factory is null", instrName.c_str());
160 return USCRIPT_INVALID_PARAM;
161 }
162
163 // Create instruction and register it
164 UScriptInstructionPtr instr = nullptr;
165 int32_t ret = factory->CreateInstructionInstance(instr, instrName);
166 if (ret != USCRIPT_SUCCESS || instr == nullptr) {
167 USCRIPT_LOGE("Fail to create instruction for %s", instrName.c_str());
168 // when instr == nullptr && ret == USCRIPT_SUCCESS, shouldn't return USCRIPT_SUCCESS
169 return ret == USCRIPT_SUCCESS ? USCRIPT_ERROR_CREATE_OBJ : USCRIPT_NOTEXIST_INSTRUCTION;
170 }
171
172 ret = AddInstruction(instrName, instr);
173 if (ret != USCRIPT_SUCCESS) {
174 USCRIPT_LOGE("Fail to add instruction for %s", instrName.c_str());
175 delete instr;
176 instr = nullptr;
177 return ret;
178 }
179
180 USCRIPT_LOGI("RegisterUserInstruction %s successfull", instrName.c_str());
181 return ret;
182 }
183 } // namespace Uscript
184