1 /*
2 * Copyright (c) 2021-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 "action_application_process.h"
17
18 #include <map>
19 #include <sys/types.h>
20 #include <dlfcn.h>
21
22 #include "constants.h"
23 #include "file_operation.h"
24 #include "system_ability_definition.h"
25 #include "if_system_ability_manager.h"
26 #include "iremote_object.h"
27 #include "singleton.h"
28 #include "securec.h"
29
30 #include "thermal_hisysevent.h"
31 #include "thermal_service.h"
32
33 using namespace OHOS::AppExecFwk;
34
35 namespace OHOS {
36 namespace PowerMgr {
37 namespace {
38 constexpr const char* PROCESS_PATH = "/data/service/el0/thermal/config/process_ctrl";
39 const int MAX_PATH = 256;
40 const int ERR_FAILED = -1;
41 }
42
ActionApplicationProcess(const std::string & actionName)43 ActionApplicationProcess::ActionApplicationProcess(const std::string& actionName)
44 {
45 actionName_ = actionName;
46 }
47
InitParams(const std::string & params)48 void ActionApplicationProcess::InitParams(const std::string& params)
49 {
50 (void)params;
51 }
52
SetStrict(bool enable)53 void ActionApplicationProcess::SetStrict(bool enable)
54 {
55 isStrict_ = enable;
56 }
57
SetEnableEvent(bool enable)58 void ActionApplicationProcess::SetEnableEvent(bool enable)
59 {
60 enableEvent_ = enable;
61 }
62
AddActionValue(uint32_t actionId,std::string value)63 void ActionApplicationProcess::AddActionValue(uint32_t actionId, std::string value)
64 {
65 if (value.empty()) {
66 return;
67 }
68 if (actionId > 0) {
69 auto iter = policyActionMap_.find(actionId);
70 if (iter != policyActionMap_.end()) {
71 iter->second.uintDelayValue = static_cast<uint32_t>(static_cast<uint32_t>(strtol(value.c_str(),
72 nullptr, STRTOL_FORMART_DEC)));
73 }
74 } else {
75 valueList_.push_back(static_cast<uint32_t>(strtol(value.c_str(), nullptr, STRTOL_FORMART_DEC)));
76 }
77 }
78
ExecuteInner()79 void ActionApplicationProcess::ExecuteInner()
80 {
81 auto tms = ThermalService::GetInstance();
82 THERMAL_RETURN_IF (tms == nullptr);
83 for (auto &policyAction : policyActionMap_) {
84 if (policyAction.second.isCompleted) {
85 valueList_.push_back(policyAction.second.uintDelayValue);
86 }
87 }
88
89 uint32_t value = GetActionValue();
90 if (value != lastValue_) {
91 if (!tms->GetSimulationXml()) {
92 ProcessAppActionRequest(value);
93 } else {
94 ProcessAppActionExecution(value);
95 }
96 WriteActionTriggeredHiSysEvent(enableEvent_, actionName_, value);
97 tms->GetObserver()->SetDecisionValue(actionName_, std::to_string(value));
98 lastValue_ = value;
99 }
100 valueList_.clear();
101 }
102
ResetActionValue()103 void ActionApplicationProcess::ResetActionValue()
104 {
105 lastValue_ = 0;
106 }
107
GetActionValue()108 uint32_t ActionApplicationProcess::GetActionValue()
109 {
110 uint32_t value = FALLBACK_VALUE_UINT_ZERO;
111 if (!valueList_.empty()) {
112 if (isStrict_) {
113 value = *min_element(valueList_.begin(), valueList_.end());
114 } else {
115 value = *max_element(valueList_.begin(), valueList_.end());
116 }
117 }
118 return value;
119 }
120
KillApplicationAction(const std::string & bundleName)121 ErrCode ActionApplicationProcess::KillApplicationAction(const std::string& bundleName)
122 {
123 typedef ErrCode(*KillApplicationActionFunc)(const std::string& bundleName);
124 void* handler = dlopen("libthermal_ability.z.so", RTLD_LAZY | RTLD_NODELETE);
125 if (handler == nullptr) {
126 THERMAL_HILOGE(COMP_SVC, "dlopen KillApplicationActionFunc failed, reason : %{public}s", dlerror());
127 return ERR_FAILED;
128 }
129 KillApplicationActionFunc KillApplicationAction =
130 reinterpret_cast<KillApplicationActionFunc>(dlsym(handler, "KillApplicationAction"));
131 if (KillApplicationAction == nullptr) {
132 THERMAL_HILOGE(COMP_SVC, "KillApplicationActionFunc is null, reason : %{public}s", dlerror());
133 #ifndef FUZZ_TEST
134 dlclose(handler);
135 #endif
136 handler = nullptr;
137 return ERR_FAILED;
138 }
139 ErrCode ret = KillApplicationAction(bundleName);
140 #ifndef FUZZ_TEST
141 dlclose(handler);
142 #endif
143 handler = nullptr;
144 return ret;
145 }
146
ProcessAppActionRequest(const uint32_t & value)147 void ActionApplicationProcess::ProcessAppActionRequest(const uint32_t& value)
148 {
149 typedef void(*ProcessAppActionRequestFunc)(const uint32_t& value);
150 void* handler = dlopen("libthermal_ability.z.so", RTLD_LAZY | RTLD_NODELETE);
151 if (handler == nullptr) {
152 THERMAL_HILOGE(COMP_SVC, "dlopen ProcessAppActionRequestFunc failed, reason : %{public}s", dlerror());
153 return;
154 }
155 ProcessAppActionRequestFunc ProcessAppActionRequest =
156 reinterpret_cast<ProcessAppActionRequestFunc>(dlsym(handler, "ProcessAppActionRequest"));
157 if (ProcessAppActionRequest == nullptr) {
158 THERMAL_HILOGE(COMP_SVC, "ProcessAppActionRequestFunc is null, reason : %{public}s", dlerror());
159 #ifndef FUZZ_TEST
160 dlclose(handler);
161 #endif
162 handler = nullptr;
163 return;
164 }
165 ProcessAppActionRequest(value);
166 #ifndef FUZZ_TEST
167 dlclose(handler);
168 #endif
169 handler = nullptr;
170 return;
171 }
172
ProcessAppActionExecution(const uint32_t & value)173 void ActionApplicationProcess::ProcessAppActionExecution(const uint32_t& value)
174 {
175 int32_t ret = -1;
176 char processBuf[MAX_PATH] = {0};
177 ret = snprintf_s(processBuf, MAX_PATH, sizeof(processBuf) - 1, PROCESS_PATH);
178 if (ret < EOK) {
179 return;
180 }
181 std::string valueString = std::to_string(value) + "\n";
182 ret = FileOperation::WriteFile(processBuf, valueString, valueString.length());
183 if (ret != EOK) {
184 return;
185 }
186 }
187 } // namespace PowerMgr
188 } // namespace OHOS
189