1 /*
2  * Copyright (c) 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 "watchdog.h"
17 
18 #include "iservice_registry.h"
19 #include "system_ability_definition.h"
20 
21 #include "background_task_mgr_service.h"
22 #include "hisysevent.h"
23 #include "transient_task_log.h"
24 
25 using namespace std;
26 
27 namespace OHOS {
28 namespace BackgroundTaskMgr {
29 
Watchdog(const wptr<BackgroundTaskMgrService> & service,const std::shared_ptr<DecisionMaker> & decision,const std::shared_ptr<AppExecFwk::EventRunner> & runner)30 Watchdog::Watchdog(const wptr<BackgroundTaskMgrService>& service, const std::shared_ptr<DecisionMaker>& decision,
31     const std::shared_ptr<AppExecFwk::EventRunner>& runner) : service_(service), decision_(decision)
32 {
33     if (runner != nullptr) {
34         SetEventRunner(runner);
35     }
36 }
37 
AddWatchdog(int32_t requestId,const std::shared_ptr<KeyInfo> & info,int32_t interval)38 bool Watchdog::AddWatchdog(int32_t requestId, const std::shared_ptr<KeyInfo>& info, int32_t interval)
39 {
40     BGTASK_LOGD("AddWatchdog %{public}d", requestId);
41     return SendEvent(requestId, info, interval);
42 }
43 
RemoveWatchdog(int32_t requestId)44 void Watchdog::RemoveWatchdog(int32_t requestId)
45 {
46     BGTASK_LOGD("RemoveWatchdog %{public}d", requestId);
47     RemoveEvent(requestId);
48 }
49 
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)50 void Watchdog::ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event)
51 {
52     if (event == nullptr) {
53         return;
54     }
55     int32_t requestId = event->GetInnerEventId();
56     const shared_ptr<KeyInfo>& info = event->GetSharedObject<KeyInfo>();
57     if (info == nullptr) {
58         return;
59     }
60     auto bgTask = service_.promote();
61     if (bgTask == nullptr) {
62         BGTASK_LOGE("fail to get bgTask service.");
63         return;
64     }
65     BGTASK_LOGI("handle watchdog, force cancel requestId: %{public}d", requestId);
66     bgTask->ForceCancelSuspendDelay(requestId);
67 }
68 
KillApplicationByUid(const std::string & bundleName,const int32_t uid,const int32_t pid)69 bool Watchdog::KillApplicationByUid(const std::string &bundleName, const int32_t uid, const int32_t pid)
70 {
71     BGTASK_LOGW("kill running application, app name is %{public}s, uid is %{public}d, pid is %{public}d",
72         bundleName.c_str(), uid, pid);
73     if (appMgrClient_ == nullptr) {
74         appMgrClient_ = std::make_unique<AppExecFwk::AppMgrClient>();
75         if (appMgrClient_ == nullptr) {
76             BGTASK_LOGE("failed to get appMgrClient");
77             return false;
78         }
79         int32_t result = static_cast<int32_t>(appMgrClient_->ConnectAppMgrService());
80         if (result != ERR_OK) {
81             BGTASK_LOGE("failed to ConnectAppMgrService");
82             return false;
83         }
84     }
85     int32_t ret = (int32_t)appMgrClient_->KillApplicationByUid(bundleName, uid);
86     if (ret != ERR_OK) {
87         BGTASK_LOGE("Fail to kill application by uid.");
88         return false;
89     }
90     HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::FRAMEWORK,
91         "PROCESS_KILL", HiviewDFX::HiSysEvent::EventType::FAULT,
92         "PID", pid,
93         "PROCESS_NAME", bundleName,
94         "MSG", "TRANSIENT_TASK_TIMEOUT",
95         "FOREGROUND", false);
96     return true;
97 }
98 }  // namespace BackgroundTaskMgr
99 }  // namespace OHOS