• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_args_child_process.h"
17 
18 #include "child_process_args_manager.h"
19 #include <dlfcn.h>
20 #include "hilog_tag_wrapper.h"
21 #include "securec.h"
22 
23 namespace OHOS {
24 namespace AbilityRuntime {
25 
Create()26 std::shared_ptr<ChildProcess> NativeArgsChildProcess::Create()
27 {
28     return std::make_shared<NativeArgsChildProcess>();
29 }
30 
~NativeArgsChildProcess()31 NativeArgsChildProcess::~NativeArgsChildProcess()
32 {
33     UnloadNativeLib();
34 }
35 
Init(const std::shared_ptr<ChildProcessStartInfo> & info)36 bool NativeArgsChildProcess::Init(const std::shared_ptr<ChildProcessStartInfo> &info)
37 {
38     TAG_LOGD(AAFwkTag::PROCESSMGR, "NativeArgsChildProcess init called.");
39     if (info == nullptr) {
40         TAG_LOGE(AAFwkTag::PROCESSMGR, "null info");
41         return false;
42     }
43 
44     if (!ChildProcess::Init(info)) {
45         TAG_LOGE(AAFwkTag::PROCESSMGR, "base class init failed");
46         return false;
47     }
48 
49     return LoadNativeLib(info);
50 }
51 
OnStart(std::shared_ptr<AppExecFwk::ChildProcessArgs> args)52 void NativeArgsChildProcess::OnStart(std::shared_ptr<AppExecFwk::ChildProcessArgs> args)
53 {
54     if (args == nullptr) {
55         TAG_LOGE(AAFwkTag::PROCESSMGR, "null args");
56         return;
57     }
58     ChildProcess::OnStart(args);
59 
60     TAG_LOGI(AAFwkTag::PROCESSMGR, "Enter native lib entry function");
61     auto nativeArgs = ParseToNativeArgs(args->entryParams, args->fds);
62 
63     if (!entryFunc_) {
64         TAG_LOGE(AAFwkTag::PROCESSMGR, "null entryFunc");
65         return;
66     }
67     ChildProcessArgsManager::GetInstance().SetChildProcessArgs(nativeArgs);
68     entryFunc_(nativeArgs);
69     TAG_LOGI(AAFwkTag::PROCESSMGR, "Native lib entry function returned");
70 }
71 
ParseToNativeArgs(const std::string & entryParams,const std::map<std::string,int32_t> & fds)72 NativeChildProcess_Args NativeArgsChildProcess::ParseToNativeArgs(const std::string &entryParams,
73     const std::map<std::string, int32_t> &fds)
74 {
75     NativeChildProcess_Args args;
76     args.fdList.head = nullptr;
77     args.entryParams = new(std::nothrow) char[entryParams.size() + 1];
78     if (!args.entryParams) {
79         TAG_LOGE(AAFwkTag::PROCESSMGR, "null entryParams");
80         return args;
81     }
82     if (strcpy_s(args.entryParams, entryParams.size() + 1, entryParams.c_str()) != ERR_OK) {
83         delete[] args.entryParams;
84         args.entryParams = nullptr;
85         TAG_LOGE(AAFwkTag::APPKIT, "strcpy_s failed");
86         return args;
87     }
88     NativeChildProcess_Fd *tail = nullptr;
89     for (const auto &fd : fds) {
90         auto &fdName = fd.first;
91         auto fdValue = fd.second;
92 
93         NativeChildProcess_Fd *node = new(std::nothrow) NativeChildProcess_Fd;
94         if (!node) {
95             TAG_LOGE(AAFwkTag::PROCESSMGR, "null node");
96             return args;
97         }
98         node->next = nullptr;
99         node->fdName = new char[fdName.size() + 1];
100         if (strcpy_s(node->fdName, fdName.size() + 1, fdName.c_str()) != ERR_OK) {
101             delete[] node->fdName;
102             node->fdName = nullptr;
103             delete node;
104             node = nullptr;
105             TAG_LOGE(AAFwkTag::APPKIT, "strcpy_s failed");
106             return args;
107         }
108         node->fd = fdValue;
109 
110         if (!args.fdList.head) {
111             args.fdList.head = node;
112         } else {
113             tail->next = node;
114         }
115         tail = node;
116     }
117     return args;
118 }
119 
LoadNativeLib(const std::shared_ptr<ChildProcessStartInfo> & info)120 bool NativeArgsChildProcess::LoadNativeLib(const std::shared_ptr<ChildProcessStartInfo> &info)
121 {
122     if (info == nullptr) {
123         TAG_LOGE(AAFwkTag::PROCESSMGR, "null info");
124         return false;
125     }
126     TAG_LOGI(AAFwkTag::PROCESSMGR, "LoadNativeLib, moduleName:%{public}s, srcEntry:%{public}s, entryFunc:%{public}s",
127         info->moduleName.c_str(), info->srcEntry.c_str(), info->entryFunc.c_str());
128     if (nativeLibHandle_ != nullptr) {
129         TAG_LOGE(AAFwkTag::PROCESSMGR, "null nativeLibHandle_");
130         return false;
131     }
132 
133     if (info->moduleName.empty()) {
134         TAG_LOGE(AAFwkTag::PROCESSMGR, "moduleName empty");
135         return false;
136     }
137 
138     Dl_namespace dlnsApp;
139     std::string appDlNameSpace = "moduleNs_" + info->moduleName;
140     int ret = dlns_get(appDlNameSpace.c_str(), &dlnsApp);
141     if (ret != 0) {
142         TAG_LOGE(AAFwkTag::PROCESSMGR, "Get app dlNamespace(%{private}s) failed, err:%{public}d",
143             appDlNameSpace.c_str(), ret);
144         return false;
145     }
146 
147     void *libHandle = dlopen_ns(&dlnsApp, info->srcEntry.c_str(), RTLD_LAZY);
148     if (libHandle == nullptr) {
149         TAG_LOGE(AAFwkTag::PROCESSMGR, "Load lib file %{private}s failed, err %{public}s",
150             info->srcEntry.c_str(), dlerror());
151         return false;
152     }
153 
154     auto entryFunc = reinterpret_cast<NativeArgsChildProcess_EntryFunc>(dlsym(libHandle, info->entryFunc.c_str()));
155     if (entryFunc == nullptr) {
156         TAG_LOGE(AAFwkTag::PROCESSMGR, "null entryFunc, err %{public}s", dlerror());
157         dlclose(libHandle);
158         return false;
159     }
160 
161     entryFunc_ = entryFunc;
162     nativeLibHandle_ = libHandle;
163     return true;
164 }
165 
UnloadNativeLib()166 void NativeArgsChildProcess::UnloadNativeLib()
167 {
168     if (nativeLibHandle_ != nullptr) {
169         dlclose(nativeLibHandle_);
170         nativeLibHandle_ = nullptr;
171         entryFunc_ = nullptr;
172     }
173 }
174 
175 } // namespace AbilityRuntime
176 } // namespace OHOS