1 /*
2 * Copyright (c) 2025 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 "app_manager_utils.h"
17
18 #include <csignal>
19 #include "thermal_log.h"
20
21 using namespace OHOS::AppExecFwk;
22
23 namespace OHOS {
24 namespace PowerMgr {
25 namespace {
26 const int32_t SIGNAL_KILL = 9;
27 }
28
AppManagerUtils()29 AppManagerUtils::AppManagerUtils()
30 {
31 if (appMgrClient_ == nullptr) {
32 appMgrClient_ = std::make_unique<AppMgrClient>();
33 }
34 }
35
KillApplicationAction(const std::string & bundleName)36 ErrCode AppManagerUtils::KillApplicationAction(const std::string& bundleName)
37 {
38 int result = ERR_OK;
39 result = appMgrClient_->KillApplication(bundleName);
40 if (result == ERR_OK) {
41 THERMAL_HILOGE(COMP_SVC, "kill application:%{public}s successfully.", bundleName.c_str());
42 } else {
43 THERMAL_HILOGE(COMP_SVC, "failed to kill application:%{public}s.", bundleName.c_str());
44 }
45 return result;
46 }
47
GetRunningProcessInfo(std::vector<RunningProcessInfo> & info)48 ErrCode AppManagerUtils::GetRunningProcessInfo(std::vector<RunningProcessInfo>& info)
49 {
50 ErrCode result = ERR_OK;
51 result = appMgrClient_->GetAllRunningProcesses(info);
52 if (result == ERR_OK) {
53 THERMAL_HILOGI(COMP_SVC, "get running process info successfully.");
54 } else {
55 THERMAL_HILOGE(COMP_SVC, "failed to get running process info.");
56 }
57 return result;
58 }
59
KillProcess(const pid_t pid)60 ErrCode AppManagerUtils::KillProcess(const pid_t pid)
61 {
62 int32_t ret = -1;
63 if (pid > 0) {
64 ret = kill(pid, SIGNAL_KILL);
65 if (ret == ERR_OK) {
66 THERMAL_HILOGI(COMP_SVC, "KillProcess: success kill, pid=%{public}d", pid);
67 } else {
68 THERMAL_HILOGE(COMP_SVC, "KillProcess: failed to kill, pid=%{public}d", pid);
69 }
70 }
71 return ret;
72 }
73
GetAppProcessInfoByName(const std::string & processName)74 RunningProcessInfo AppManagerUtils::GetAppProcessInfoByName(const std::string& processName)
75 {
76 RunningProcessInfo appProcessInfo;
77 appProcessInfo.pid_ = 0;
78 if (ERR_OK == GetRunningProcessInfo(allAppProcessInfos_)) {
79 const auto& it = std::find_if(allAppProcessInfos_.begin(), allAppProcessInfos_.end(), [&](const auto& info) {
80 return processName == info.processName_;
81 });
82 appProcessInfo = (it != allAppProcessInfos_.end()) ? *it : appProcessInfo;
83 }
84 return appProcessInfo;
85 }
86
GetAllRunnningAppProcess()87 void AppManagerUtils::GetAllRunnningAppProcess()
88 {
89 allAppProcessInfos_.clear();
90 bgAppProcessInfos_.clear();
91 fgAppProcessInfos_.clear();
92 if (ERR_OK == GetRunningProcessInfo(allAppProcessInfos_)) {
93 for (const auto& info : allAppProcessInfos_) {
94 if (info.state_ == AppProcessState::APP_STATE_BACKGROUND) {
95 bgAppProcessInfos_.push_back(info);
96 } else if (info.state_ == AppProcessState::APP_STATE_FOREGROUND) {
97 fgAppProcessInfos_.push_back(info);
98 }
99 }
100 }
101 THERMAL_HILOGD(COMP_SVC, "GetAllRunnningAppProcess allAppProcessInfos_ size = %{public}d, bgAppProcessInfos_ "
102 "size = %{public}d, fgAppProcessInfos_ size = %{public}d", static_cast<int>(allAppProcessInfos_.size()),
103 static_cast<int>(bgAppProcessInfos_.size()), static_cast<int>(fgAppProcessInfos_.size()));
104 }
105
KillBgAppProcess()106 void AppManagerUtils::KillBgAppProcess()
107 {
108 for (auto bg : bgAppProcessInfos_) {
109 if (KillProcess(bg.pid_) != ERR_OK) {
110 THERMAL_HILOGE(COMP_SVC, "failed to kill bg process");
111 }
112 }
113 }
114
KillFgAppProcess()115 void AppManagerUtils::KillFgAppProcess()
116 {
117 for (auto fg : fgAppProcessInfos_) {
118 if (KillProcess(fg.pid_) != ERR_OK) {
119 THERMAL_HILOGE(COMP_SVC, "failed to kill fg process");
120 }
121 }
122 }
123
KillAllAppProcess()124 void AppManagerUtils::KillAllAppProcess()
125 {
126 for (auto all : allAppProcessInfos_) {
127 if (KillProcess(all.pid_) != ERR_OK) {
128 THERMAL_HILOGE(COMP_SVC, "failed to kill all process");
129 }
130 }
131 }
132
ProcessAppActionRequest(const uint32_t & value)133 void AppManagerUtils::ProcessAppActionRequest(const uint32_t& value)
134 {
135 THERMAL_HILOGD(COMP_SVC, "value: %{public}d", value);
136 GetAllRunnningAppProcess();
137 switch (value) {
138 case KILL_FG_PROCESS_APP: {
139 KillFgAppProcess();
140 break;
141 }
142 case KILL_BG_PROCESS_APP: {
143 KillBgAppProcess();
144 break;
145 }
146 case KILL_ALL_PROCESS_APP: {
147 KillAllAppProcess();
148 break;
149 }
150 default: {
151 break;
152 }
153 }
154 }
155
KillApplicationAction(const std::string & bundleName)156 extern "C" API ErrCode KillApplicationAction(const std::string& bundleName)
157 {
158 return AppManagerUtils::GetInstance().KillApplicationAction(bundleName);
159 }
160
ProcessAppActionRequest(const uint32_t & value)161 extern "C" API void ProcessAppActionRequest(const uint32_t& value)
162 {
163 AppManagerUtils::GetInstance().ProcessAppActionRequest(value);
164 }
165 } // namespace PowerMgr
166 } // namespace OHOS