1 /*
2 * Copyright (c) 2023 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
16 #include "module_update_main.h"
17
18 #include "hisysevent_manager.h"
19 #include "log/log.h"
20 #include "module_update_service.h"
21 #include "parameter.h"
22 #include "sys_event_service_listener.h"
23 #include "system_ability_definition.h"
24
25 #include <unistd.h>
26
27 namespace OHOS {
28 namespace SysInstaller {
29 using namespace HiviewDFX;
30 using namespace Updater;
31
32 namespace {
33 constexpr const char *BOOT_COMPLETE_PARAM = "bootevent.boot.completed";
34 constexpr const char *BOOT_SUCCESS_VALUE = "true";
35 constexpr int32_t PARAM_VALUE_SIZE = 10;
36 constexpr int32_t RETRY_TIMES_FOR_SAMGR = 10;
37 constexpr std::chrono::milliseconds MILLISECONDS_WAITING_SAMGR_ONE_TIME(100);
38 }
39
ModuleUpdateMain()40 ModuleUpdateMain::ModuleUpdateMain()
41 {
42 moduleUpdate_ = new ModuleUpdateService();
43 moduleUpdate_->ScanPreInstalledHmp();
44 }
45
46 ModuleUpdateMain::~ModuleUpdateMain() = default;
47
RegisterModuleUpdateService()48 bool ModuleUpdateMain::RegisterModuleUpdateService()
49 {
50 LOG(INFO) << "RegisterModuleUpdateService";
51 auto samgr = GetSystemAbilityManager();
52 if (samgr == nullptr) {
53 LOG(ERROR) << "Failed to get system ability manager";
54 return false;
55 }
56 int32_t ret = samgr->AddSystemAbility(MODULE_UPDATE_SERVICE_ID, moduleUpdate_);
57 if (ret != 0) {
58 LOG(ERROR) << "AddSystemAbility error " << ret;
59 return false;
60 }
61 return true;
62 }
63
WaitForSysEventService()64 bool ModuleUpdateMain::WaitForSysEventService()
65 {
66 LOG(INFO) << "WaitForSysEventService";
67 auto samgr = GetSystemAbilityManager();
68 if (samgr == nullptr) {
69 LOG(ERROR) << "Failed to get system ability manager";
70 return false;
71 }
72 int32_t ret = samgr->SubscribeSystemAbility(DFX_SYS_EVENT_SERVICE_ABILITY_ID, new SysEventServiceListener());
73 if (ret != 0) {
74 LOG(ERROR) << "SubscribeSystemAbility error " << ret;
75 return false;
76 }
77 return true;
78 }
79
CheckBootComplete() const80 bool ModuleUpdateMain::CheckBootComplete() const
81 {
82 char value[PARAM_VALUE_SIZE] = "";
83 int ret = GetParameter(BOOT_COMPLETE_PARAM, "", value, PARAM_VALUE_SIZE);
84 if (ret < 0) {
85 LOG(ERROR) << "Failed to get parameter " << BOOT_COMPLETE_PARAM;
86 return false;
87 }
88 return strcmp(value, BOOT_SUCCESS_VALUE) == 0;
89 }
90
WatchBootComplete() const91 void ModuleUpdateMain::WatchBootComplete() const
92 {
93 LOG(INFO) << "WatchBootComplete";
94 int ret = WatchParameter(BOOT_COMPLETE_PARAM, BootCompleteCallback, nullptr);
95 if (ret == -1) {
96 LOG(ERROR) << "Failed to watch parameter " << BOOT_COMPLETE_PARAM;
97 }
98 }
99
RegisterSysEventListener()100 bool ModuleUpdateMain::RegisterSysEventListener()
101 {
102 LOG(INFO) << "RegisterSysEventListener";
103 crashListener_ = std::make_shared<CrashSysEventListener>();
104 std::vector<ListenerRule> rules;
105 rules.emplace_back(CRASH_DOMAIN, CRASH_NAME, "", RuleType::WHOLE_WORD, static_cast<uint32_t>(CRASH_TYPE));
106 int32_t ret = HiSysEventManager::AddListener(crashListener_, rules);
107 if (ret != 0) {
108 LOG(ERROR) << "HiSysEventManager::AddListener error " << ret;
109 return false;
110 }
111 return true;
112 }
113
OnSysEventServiceDied()114 void ModuleUpdateMain::OnSysEventServiceDied()
115 {
116 crashListener_ = nullptr;
117 }
118
OnProcessCrash(const std::string & processName)119 void ModuleUpdateMain::OnProcessCrash(const std::string &processName)
120 {
121 LOG(INFO) << "OnProcessCrash " << processName;
122 moduleUpdate_->OnProcessCrash(processName);
123 }
124
BootCompleteCallback(const char * key,const char * value,void * context)125 void ModuleUpdateMain::BootCompleteCallback(const char *key, const char *value, void *context)
126 {
127 LOG(INFO) << "BootCompleteCallback key=" << key << ", value=" << value;
128 if (strcmp(key, BOOT_COMPLETE_PARAM) != 0 || strcmp(value, BOOT_SUCCESS_VALUE) != 0) {
129 return;
130 }
131 ModuleUpdateMain::GetInstance().OnBootCompleted();
132 }
133
OnBootCompleted()134 void ModuleUpdateMain::OnBootCompleted()
135 {
136 LOG(INFO) << "OnBootCompleted";
137 if (crashListener_ != nullptr) {
138 int32_t ret = HiSysEventManager::RemoveListener(crashListener_);
139 if (ret != 0) {
140 LOG(ERROR) << "HiSysEventManager::RemoveListener error " << ret;
141 }
142 }
143 moduleUpdate_->OnBootCompleted();
144 }
145
GetSystemAbilityManager()146 sptr<ISystemAbilityManager> &ModuleUpdateMain::GetSystemAbilityManager()
147 {
148 if (samgr_ != nullptr) {
149 return samgr_;
150 }
151 int32_t times = RETRY_TIMES_FOR_SAMGR;
152 constexpr int32_t duration = std::chrono::microseconds(MILLISECONDS_WAITING_SAMGR_ONE_TIME).count();
153 while (times > 0) {
154 times--;
155 samgr_ = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
156 if (samgr_ == nullptr) {
157 LOG(INFO) << "waiting for samgr";
158 usleep(duration);
159 } else {
160 break;
161 }
162 }
163 return samgr_;
164 }
165 } // namespace SysInstaller
166 } // namespace OHOS