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 <unistd.h>
17 #include <dlfcn.h>
18
19 #include <iosfwd>
20 #include <ostream>
21 #include <sstream>
22 #include <string>
23
24 #include "hilog/log_cpp.h"
25 #include "nweb_test_log.h"
26
27 using namespace OHOS;
28
main(int argc,const char * argv[])29 int main(int argc, const char* argv[])
30 {
31 TESTLOG_I("nweb subprocess %{public}s begin, pid=%{public}d", argv[0], getpid());
32 #ifdef __MUSL__
33 Dl_namespace dlns;
34 dlns_init(&dlns, "nweb_test_ns");
35 dlns_create(&dlns, "/data/app/el1/bundle/public/com.ohos.nweb/libs/arm");
36 const std::string LIB_PATH_NWEB_RENDER = "libnweb_render.so";
37 const std::string LIB_PATH_WEB_ENGINE = "libweb_engine.so";
38 void *libHandleWebEngine = dlopen_ns(&dlns, LIB_PATH_WEB_ENGINE.c_str(), RTLD_NOW);
39 if (libHandleWebEngine == nullptr) {
40 TESTLOG_E("fail to dlopen %{public}s, errmsg=%{public}s", LIB_PATH_WEB_ENGINE.c_str(), dlerror());
41 return -1;
42 }
43 void *libHandleNWebRender = dlopen_ns(&dlns, LIB_PATH_NWEB_RENDER.c_str(), RTLD_NOW);
44 if (libHandleNWebRender == nullptr) {
45 TESTLOG_E("fail to dlopen %{public}s, errmsg=%{public}s", LIB_PATH_NWEB_RENDER.c_str(), dlerror());
46 return -1;
47 }
48 #else
49 const std::string LOAD_LIB_DIR_INSTALLATION = "/data/app/el1/bundle/public/com.ohos.nweb/libs/arm";
50 const std::string LIB_PATH_WEB_ENGINE = LOAD_LIB_DIR_INSTALLATION + "/libweb_engine.so";
51 const std::string LIB_PATH_NWEB_RENDER = LOAD_LIB_DIR_INSTALLATION + "/libnweb_render.so";
52 void *libHandleWebEngine = ::dlopen(LIB_PATH_WEB_ENGINE.c_str(), RTLD_NOW);
53 if (libHandleWebEngine == nullptr) {
54 TESTLOG_E("fail to dlopen %{public}s, errmsg=%{public}s", LIB_PATH_WEB_ENGINE.c_str(), dlerror());
55 return -1;
56 }
57 void *libHandleNWebRender = ::dlopen(LIB_PATH_NWEB_RENDER.c_str(), RTLD_NOW);
58 if (libHandleNWebRender == nullptr) {
59 TESTLOG_E("fail to dlopen %{public}s, errmsg=%{public}s", LIB_PATH_NWEB_RENDER.c_str(), dlerror());
60 return -1;
61 }
62 #endif
63 using NWebRenderMainFunc = void (*)(const char* args);
64 const std::string NWEB_RENDER_MAIN = "NWebRenderMain";
65 NWebRenderMainFunc func =
66 reinterpret_cast<NWebRenderMainFunc>(dlsym(libHandleNWebRender, NWEB_RENDER_MAIN.c_str()));
67 if (func == nullptr) {
68 TESTLOG_E("fail to dlsym %{public}s from libnweb_render.so", NWEB_RENDER_MAIN.c_str());
69 return -1;
70 }
71 std::stringstream argv_ss;
72 const char separator = '#';
73 for (int i = 0; i < argc - 1; ++i) {
74 argv_ss << argv[i] << separator;
75 }
76 argv_ss << argv[argc - 1];
77 func(argv_ss.str().data());
78 if (libHandleWebEngine != nullptr) {
79 ::dlclose(libHandleWebEngine);
80 libHandleWebEngine = nullptr;
81 }
82 if (libHandleNWebRender != nullptr) {
83 ::dlclose(libHandleNWebRender);
84 libHandleNWebRender = nullptr;
85 }
86
87 TESTLOG_I("nweb subprocess %{public}s end, pid=%{public}d", argv[0], getpid());
88 }
89