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 <csignal>
19 #include <map>
20 #include <sys/types.h>
21
22 #include "constants.h"
23 #include "file_operation.h"
24 #include "sa_mgr_client.h"
25 #include "system_ability_definition.h"
26 #include "if_system_ability_manager.h"
27 #include "iremote_object.h"
28 #include "singleton.h"
29 #include "securec.h"
30
31 #include "thermal_hisysevent.h"
32 #include "thermal_service.h"
33
34 using namespace OHOS::AppExecFwk;
35
36 namespace OHOS {
37 namespace PowerMgr {
38 namespace {
39 const int32_t SIGNAL_KILL = 9;
40 auto g_service = DelayedSpSingleton<ThermalService>::GetInstance();
41 constexpr const char* PROCESS_PATH = "/data/service/el0/thermal/config/process_ctrl";
42 const int MAX_PATH = 256;
43 }
44
ActionApplicationProcess(const std::string & actionName)45 ActionApplicationProcess::ActionApplicationProcess(const std::string& actionName)
46 {
47 actionName_ = actionName;
48 }
49
Init()50 bool ActionApplicationProcess::Init()
51 {
52 if (appMgrClient_ == nullptr) {
53 appMgrClient_ = std::make_unique<AppMgrClient>();
54 }
55 return true;
56 }
57
InitParams(const std::string & params)58 void ActionApplicationProcess::InitParams(const std::string& params)
59 {
60 (void)params;
61 }
62
SetStrict(bool enable)63 void ActionApplicationProcess::SetStrict(bool enable)
64 {
65 isStrict_ = enable;
66 }
67
SetEnableEvent(bool enable)68 void ActionApplicationProcess::SetEnableEvent(bool enable)
69 {
70 enableEvent_ = enable;
71 }
72
AddActionValue(std::string value)73 void ActionApplicationProcess::AddActionValue(std::string value)
74 {
75 if (value.empty()) {
76 return;
77 }
78 valueList_.push_back(static_cast<uint32_t>(strtol(value.c_str(), nullptr, STRTOL_FORMART_DEC)));
79 }
80
Execute()81 void ActionApplicationProcess::Execute()
82 {
83 THERMAL_RETURN_IF (g_service == nullptr);
84 uint32_t value = GetActionValue();
85 if (value != lastValue_) {
86 if (!g_service->GetSimulationXml()) {
87 ProcessAppActionRequest(value);
88 } else {
89 ProcessAppActionExecution(value);
90 }
91 WriteActionTriggeredHiSysEvent(enableEvent_, actionName_, value);
92 g_service->GetObserver()->SetDecisionValue(actionName_, std::to_string(value));
93 lastValue_ = value;
94 }
95 valueList_.clear();
96 }
97
GetActionValue()98 uint32_t ActionApplicationProcess::GetActionValue()
99 {
100 std::string scene = g_service->GetScene();
101 auto iter = g_sceneMap.find(scene);
102 if (iter != g_sceneMap.end()) {
103 return static_cast<uint32_t>(strtol(iter->second.c_str(), nullptr, STRTOL_FORMART_DEC));
104 }
105 uint32_t value = FALLBACK_VALUE_UINT_ZERO;
106 if (!valueList_.empty()) {
107 if (isStrict_) {
108 value = *min_element(valueList_.begin(), valueList_.end());
109 } else {
110 value = *max_element(valueList_.begin(), valueList_.end());
111 }
112 }
113 return value;
114 }
115
KillApplicationAction(const std::string & bundleName)116 ErrCode ActionApplicationProcess::KillApplicationAction(const std::string& bundleName)
117 {
118 int result = ERR_OK;
119 result = appMgrClient_->KillApplication(bundleName);
120 if (result == ERR_OK) {
121 THERMAL_HILOGE(COMP_SVC, "kill application:%{public}s successfully.", bundleName.c_str());
122 } else {
123 THERMAL_HILOGE(COMP_SVC, "failed to kill application:%{public}s.", bundleName.c_str());
124 }
125 return result;
126 }
127
GetRunningProcessInfo(std::vector<RunningProcessInfo> & info)128 ErrCode ActionApplicationProcess::GetRunningProcessInfo(std::vector<RunningProcessInfo>& info)
129 {
130 ErrCode result = ERR_OK;
131 result = appMgrClient_->GetAllRunningProcesses(info);
132 if (result == ERR_OK) {
133 THERMAL_HILOGI(COMP_SVC, "get running process info successfully.");
134 } else {
135 THERMAL_HILOGE(COMP_SVC, "failed to get running process info.");
136 }
137 return result;
138 }
139
KillProcess(const pid_t pid)140 ErrCode ActionApplicationProcess::KillProcess(const pid_t pid)
141 {
142 int32_t ret = -1;
143 if (pid > 0) {
144 ret = kill(pid, SIGNAL_KILL);
145 if (ret == ERR_OK) {
146 THERMAL_HILOGI(COMP_SVC, "KillProcess: success kill, pid=%{public}d", pid);
147 } else {
148 THERMAL_HILOGE(COMP_SVC, "KillProcess: failed to kill, pid=%{public}d", pid);
149 }
150 }
151 return ret;
152 }
153
GetAppProcessInfoByName(const std::string & processName)154 RunningProcessInfo ActionApplicationProcess::GetAppProcessInfoByName(const std::string& processName)
155 {
156 RunningProcessInfo appProcessInfo;
157 appProcessInfo.pid_ = 0;
158 if (ERR_OK == GetRunningProcessInfo(allAppProcessInfos_)) {
159 const auto& it = std::find_if(allAppProcessInfos_.begin(), allAppProcessInfos_.end(), [&](const auto& info) {
160 return processName == info.processName_;
161 });
162 appProcessInfo = (it != allAppProcessInfos_.end()) ? *it : appProcessInfo;
163 }
164 return appProcessInfo;
165 }
166
167
GetAllRunnningAppProcess()168 void ActionApplicationProcess::GetAllRunnningAppProcess()
169 {
170 if (ERR_OK == GetRunningProcessInfo(allAppProcessInfos_)) {
171 for (const auto& info : allAppProcessInfos_) {
172 if (info.state_ == AppProcessState::APP_STATE_BACKGROUND) {
173 bgAppProcessInfos_.push_back(info);
174 } else if (info.state_ == AppProcessState::APP_STATE_FOREGROUND) {
175 fgAppProcessInfos_.push_back(info);
176 }
177 }
178 }
179 }
180
KillBgAppProcess()181 void ActionApplicationProcess::KillBgAppProcess()
182 {
183 for (auto bg : bgAppProcessInfos_) {
184 if (KillProcess(bg.pid_) != ERR_OK) {
185 THERMAL_HILOGE(COMP_SVC, "failed to kill bg process");
186 }
187 }
188 }
189
KillFgAppProcess()190 void ActionApplicationProcess::KillFgAppProcess()
191 {
192 for (auto fg : fgAppProcessInfos_) {
193 if (KillProcess(fg.pid_) != ERR_OK) {
194 THERMAL_HILOGE(COMP_SVC, "failed to kill fg process");
195 }
196 }
197 }
198
KillAllAppProcess()199 void ActionApplicationProcess::KillAllAppProcess()
200 {
201 for (auto all : allAppProcessInfos_) {
202 if (KillProcess(all.pid_) != ERR_OK) {
203 THERMAL_HILOGE(COMP_SVC, "failed to kill all process");
204 }
205 }
206 }
207
ProcessAppActionRequest(const uint32_t & value)208 void ActionApplicationProcess::ProcessAppActionRequest(const uint32_t& value)
209 {
210 THERMAL_HILOGD(COMP_SVC, "value: %{public}d", value);
211 GetAllRunnningAppProcess();
212 switch (value) {
213 case KILL_FG_PROCESS_APP: {
214 KillFgAppProcess();
215 break;
216 }
217 case KILL_BG_PROCESS_APP: {
218 KillBgAppProcess();
219 break;
220 }
221 case KILL_ALL_PROCESS_APP: {
222 KillAllAppProcess();
223 break;
224 }
225 default: {
226 break;
227 }
228 }
229 }
230
ProcessAppActionExecution(const uint32_t & value)231 void ActionApplicationProcess::ProcessAppActionExecution(const uint32_t& value)
232 {
233 int32_t ret = -1;
234 char processBuf[MAX_PATH] = {0};
235 ret = snprintf_s(processBuf, MAX_PATH, sizeof(processBuf) - 1, PROCESS_PATH);
236 if (ret < EOK) {
237 return;
238 }
239 std::string valueString = std::to_string(value) + "\n";
240 ret = FileOperation::WriteFile(processBuf, valueString, valueString.length());
241 if (ret != EOK) {
242 return;
243 }
244 }
245 } // namespace PowerMgr
246 } // namespace OHOS
247