1 /*
2 * Copyright (c) 2024 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 #include "bundle_app_spawn_client.h"
16
17 #include "app_log_tag_wrapper.h"
18 #include "bundle_constants.h"
19 #include "hitrace_meter.h"
20
21 namespace OHOS {
22 namespace AppExecFwk {
23 namespace {
24 constexpr int32_t MAX_PROC_NAME_LEN = 256;
25 }
26
GetInstance()27 BundleAppSpawnClient& BundleAppSpawnClient::GetInstance()
28 {
29 static BundleAppSpawnClient instance;
30 return instance;
31 }
32
BundleAppSpawnClient()33 BundleAppSpawnClient::BundleAppSpawnClient()
34 : serviceName_(APPSPAWN_SERVER_NAME),
35 handle_(nullptr),
36 state_(SpawnConnectionState::STATE_NOT_CONNECT) {}
37
~BundleAppSpawnClient()38 BundleAppSpawnClient::~BundleAppSpawnClient()
39 {
40 CloseConnection();
41 }
42
OpenConnection()43 int32_t BundleAppSpawnClient::OpenConnection()
44 {
45 std::lock_guard<std::mutex> lock(mutex_);
46 if (state_ == SpawnConnectionState::STATE_CONNECTED) {
47 return 0;
48 }
49 AppSpawnClientHandle handle = nullptr;
50 int32_t ret = AppSpawnClientInit(serviceName_.c_str(), &handle);
51 if (FAILED(ret)) {
52 LOG_E(BMS_TAG_INSTALLER, "AppSpawnClientInit failed");
53 state_ = SpawnConnectionState::STATE_CONNECT_FAILED;
54 return ret;
55 }
56 handle_ = handle;
57 state_ = SpawnConnectionState::STATE_CONNECTED;
58
59 return ret;
60 }
61
CloseConnection()62 void BundleAppSpawnClient::CloseConnection()
63 {
64 std::lock_guard<std::mutex> lock(mutex_);
65 if (state_ == SpawnConnectionState::STATE_CONNECTED) {
66 AppSpawnClientDestroy(handle_);
67 handle_ = nullptr;
68 }
69 state_ = SpawnConnectionState::STATE_NOT_CONNECT;
70 }
71
72
AppspawnCreateDefaultMsg(const AppSpawnRemoveSandboxDirMsg & removeSandboxDirMsg,AppSpawnReqMsgHandle reqHandle)73 int32_t BundleAppSpawnClient::AppspawnCreateDefaultMsg(const AppSpawnRemoveSandboxDirMsg &removeSandboxDirMsg,
74 AppSpawnReqMsgHandle reqHandle)
75 {
76 int32_t ret = 0;
77 do {
78 ret = AppSpawnReqMsgSetBundleInfo(reqHandle, removeSandboxDirMsg.bundleIndex,
79 removeSandboxDirMsg.bundleName.c_str());
80 if (ret != 0) {
81 LOG_E(BMS_TAG_INSTALLER, "AppSpawnReqMsgSetBundleInfo fail, ret: %{public}d", ret);
82 break;
83 }
84 AppDacInfo appDacInfo = {0};
85 appDacInfo.uid = removeSandboxDirMsg.uid;
86 ret = AppSpawnReqMsgSetAppDacInfo(reqHandle, &appDacInfo);
87 if (ret != 0) {
88 LOG_E(BMS_TAG_INSTALLER, "AppSpawnReqMsgSetAppDacInfo fail, ret: %{public}d", ret);
89 break;
90 }
91 ret = AppSpawnReqMsgSetAppFlag(reqHandle, removeSandboxDirMsg.flags);
92 if (ret != 0) {
93 LOG_E(BMS_TAG_INSTALLER, "AppSpawnReqMsgSetAppFlag fail, ret: %{public}d", ret);
94 break;
95 }
96 return ret;
97 } while (0);
98
99 AppSpawnReqMsgFree(reqHandle);
100 return ret;
101 }
102
VerifyMsg(const AppSpawnRemoveSandboxDirMsg & removeSandboxDirMsg)103 bool BundleAppSpawnClient::VerifyMsg(const AppSpawnRemoveSandboxDirMsg &removeSandboxDirMsg)
104 {
105 if (removeSandboxDirMsg.code == MSG_UNINSTALL_DEBUG_HAP) {
106 if (removeSandboxDirMsg.bundleName.empty() || removeSandboxDirMsg.bundleName.size() >= MAX_PROC_NAME_LEN) {
107 LOG_E(BMS_TAG_INSTALLER, "invalid bundleName, name:%{public}s", removeSandboxDirMsg.bundleName.c_str());
108 return false;
109 }
110 if (removeSandboxDirMsg.bundleIndex < 0 ||
111 removeSandboxDirMsg.bundleIndex > Constants::INITIAL_SANDBOX_APP_INDEX) {
112 LOG_E(BMS_TAG_INSTALLER, "invalid bundleIndex %{public}d.", removeSandboxDirMsg.bundleIndex);
113 return false;
114 }
115 if (removeSandboxDirMsg.uid < 0) {
116 LOG_E(BMS_TAG_INSTALLER, "invalid uid %{public}d.", removeSandboxDirMsg.uid);
117 return false;
118 }
119 } else {
120 LOG_E(BMS_TAG_INSTALLER, "invalid code %{public}d.", removeSandboxDirMsg.code);
121 return false;
122 }
123 return true;
124 }
125
RemoveSandboxDir(const AppSpawnRemoveSandboxDirMsg & removeSandboxDirMsg)126 int32_t BundleAppSpawnClient::RemoveSandboxDir(const AppSpawnRemoveSandboxDirMsg &removeSandboxDirMsg)
127 {
128 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
129 LOG_D(BMS_TAG_INSTALLER, "call RemoveSandboxDir start");
130 if (!VerifyMsg(removeSandboxDirMsg)) {
131 LOG_E(BMS_TAG_INSTALLER, "invalid msg");
132 return ERR_INVALID_VALUE;
133 }
134
135 int32_t ret = 0;
136 AppSpawnReqMsgHandle reqHandle = nullptr;
137
138 ret = OpenConnection();
139 if (ret != 0) {
140 LOG_E(BMS_TAG_INSTALLER, "BundleAppSpawnClient open connection fail");
141 return ret;
142 }
143
144 ret = AppSpawnReqMsgCreate(static_cast<AppSpawnMsgType>(removeSandboxDirMsg.code),
145 removeSandboxDirMsg.bundleName.c_str(), &reqHandle);
146 if (ret != 0) {
147 LOG_E(BMS_TAG_INSTALLER, "AppSpawnReqMsgCreate fail");
148 return ret;
149 }
150
151 ret = AppspawnCreateDefaultMsg(removeSandboxDirMsg, reqHandle);
152 if (ret != 0) {
153 LOG_E(BMS_TAG_INSTALLER, "AppspawnCreateDefaultMsg fail");
154 return ret; // create msg failed
155 }
156
157 AppSpawnResult result = {0};
158 ret = AppSpawnClientSendMsg(handle_, reqHandle, &result);
159 if (ret != 0) {
160 LOG_E(BMS_TAG_INSTALLER, "BundleAppSpawnClient appspawn send msg fail");
161 }
162 LOG_D(BMS_TAG_INSTALLER, "call RemoveSandboxDir end");
163 return ret;
164 }
165 } // namespace AppExecFwk
166 } // namespace OHOS