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
16 #include "native_child_ipc_process.h"
17 #include <dlfcn.h>
18 #include <thread>
19 #include <chrono>
20 #include "hilog_tag_wrapper.h"
21 #include "child_process_manager_error_utils.h"
22
23 namespace OHOS {
24 namespace AbilityRuntime {
25
Create()26 std::shared_ptr<ChildProcess> NativeChildIpcProcess::Create()
27 {
28 return std::make_shared<NativeChildIpcProcess>();
29 }
30
~NativeChildIpcProcess()31 NativeChildIpcProcess::~NativeChildIpcProcess()
32 {
33 UnloadNativeLib();
34 }
35
Init(const std::shared_ptr<ChildProcessStartInfo> & info)36 bool NativeChildIpcProcess::Init(const std::shared_ptr<ChildProcessStartInfo> &info)
37 {
38 TAG_LOGD(AAFwkTag::PROCESSMGR, "init");
39 if (info == nullptr || info->ipcObj == nullptr) {
40 TAG_LOGE(AAFwkTag::PROCESSMGR, "info or ipc callback is null");
41 return false;
42 }
43
44 if (!ChildProcess::Init(info)) {
45 TAG_LOGE(AAFwkTag::PROCESSMGR, "init failed");
46 return false;
47 }
48
49 auto iNotify = iface_cast<OHOS::AppExecFwk::INativeChildNotify>(info->ipcObj);
50 if (iNotify == nullptr) {
51 TAG_LOGE(AAFwkTag::PROCESSMGR, "null iNotify");
52 return false;
53 }
54
55 if (!LoadNativeLib(info)) {
56 iNotify->OnError(static_cast<int32_t>(ChildProcessManagerErrorCode::ERR_LIB_LOADING_FAILED));
57 return false;
58 }
59
60 mainProcessCb_ = iNotify;
61 return true;
62 }
63
OnStart()64 void NativeChildIpcProcess::OnStart()
65 {
66 if (funcNativeLibOnConnect_ == nullptr || funcNativeLibMainProc_ == nullptr || mainProcessCb_ == nullptr) {
67 TAG_LOGE(AAFwkTag::PROCESSMGR, "No init");
68 return;
69 }
70
71 ChildProcess::OnStart();
72 OHIPCRemoteStub *ipcStub = funcNativeLibOnConnect_();
73 if (ipcStub == nullptr || ipcStub->remote == nullptr) {
74 TAG_LOGE(AAFwkTag::PROCESSMGR, "null ipctub");
75 mainProcessCb_->OnError(static_cast<int32_t>(ChildProcessManagerErrorCode::ERR_CONNECTION_FAILED));
76 return;
77 }
78
79 std::thread cbThread([this, childIpcStub = std::move(ipcStub->remote)] () -> void {
80 // Wait MainProc run first
81 std::this_thread::sleep_for(std::chrono::milliseconds(1));
82 TAG_LOGI(AAFwkTag::PROCESSMGR, "Notify native child process started");
83 mainProcessCb_->OnNativeChildStarted(childIpcStub);
84 });
85
86 TAG_LOGI(AAFwkTag::PROCESSMGR, "Enter MainProc");
87 funcNativeLibMainProc_();
88 TAG_LOGI(AAFwkTag::PROCESSMGR, "MainProc returned");
89
90 if (cbThread.joinable()) {
91 cbThread.join();
92 }
93 }
94
LoadNativeLib(const std::shared_ptr<ChildProcessStartInfo> & info)95 bool NativeChildIpcProcess::LoadNativeLib(const std::shared_ptr<ChildProcessStartInfo> &info)
96 {
97 if (nativeLibHandle_ != nullptr) {
98 TAG_LOGE(AAFwkTag::PROCESSMGR, "Native lib already loaded");
99 return false;
100 }
101
102 if (info->moduleName.empty()) {
103 TAG_LOGE(AAFwkTag::PROCESSMGR, "empty module name");
104 return false;
105 }
106
107 Dl_namespace dlnsApp;
108 std::string appDlNameSpace = "moduleNs_" + info->moduleName;
109 int ret = dlns_get(appDlNameSpace.c_str(), &dlnsApp);
110 if (ret != 0) {
111 TAG_LOGE(AAFwkTag::PROCESSMGR, "Get dlns(%{private}s) err:%{public}d",
112 appDlNameSpace.c_str(), ret);
113 return false;
114 }
115
116 void *libHandle = dlopen_ns(&dlnsApp, info->srcEntry.c_str(), RTLD_LAZY);
117 if (libHandle == nullptr) {
118 TAG_LOGE(AAFwkTag::PROCESSMGR, "Load lib file %{private}s err %{public}s",
119 info->srcEntry.c_str(), dlerror());
120 return false;
121 }
122
123 do {
124 NativeChildProcess_OnConnect funcOnConnect =
125 reinterpret_cast<NativeChildProcess_OnConnect>(dlsym(libHandle, "NativeChildProcess_OnConnect"));
126 if (funcOnConnect == nullptr) {
127 TAG_LOGE(AAFwkTag::PROCESSMGR, "null funcOnConnect, err %{public}s", dlerror());
128 break;
129 }
130
131 NativeChildProcess_MainProc funcMainProc =
132 reinterpret_cast<NativeChildProcess_MainProc>(dlsym(libHandle, "NativeChildProcess_MainProc"));
133 if (funcMainProc == nullptr) {
134 TAG_LOGE(AAFwkTag::PROCESSMGR, "null funcMainProc, err %{public}s", dlerror());
135 break;
136 }
137
138 funcNativeLibOnConnect_ = funcOnConnect;
139 funcNativeLibMainProc_ = funcMainProc;
140 nativeLibHandle_ = libHandle;
141 return true;
142 } while (false);
143
144 dlclose(libHandle);
145 return false;
146 }
147
UnloadNativeLib()148 void NativeChildIpcProcess::UnloadNativeLib()
149 {
150 if (nativeLibHandle_ != nullptr) {
151 dlclose(nativeLibHandle_);
152 nativeLibHandle_ = nullptr;
153 funcNativeLibOnConnect_ = nullptr;
154 funcNativeLibMainProc_ = nullptr;
155 }
156 }
157
158 } // namespace AbilityRuntime
159 } // namespace OHOS
160