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