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