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 "napi/native_api.h"
17 #include <cerrno>
18 #include <dirent.h>
19 #include <dlfcn.h>
20 #include <fcntl.h>
21 #include <js_native_api_types.h>
22 
23 #define INIT (-1)
24 #define SUCCESS 0
25 
DlSym(napi_env env, napi_callback_info info)26 static napi_value DlSym(napi_env env, napi_callback_info info)
27 {
28     const char *path = "/system/lib/extensionability/libstatic_subscriber_extension_module.z.so";
29     void *ptr = dlopen(path, RTLD_LAZY);
30     errno = 0;
31     dlsym(ptr, "OHOS_EXTENSION_GetExtensionModule");
32     napi_value result = nullptr;
33     napi_create_int32(env, errno, &result);
34     return result;
35 }
36 
DlVSym(napi_env env, napi_callback_info info)37 static napi_value DlVSym(napi_env env, napi_callback_info info)
38 {
39     const char *path = "/system/lib/extensionability/libstatic_subscriber_extension_module.z.so";
40     void *ptr = dlopen(path, RTLD_LAZY);
41     errno = 0;
42     const char *dso_easy_symver_version_stable = "STABLE";
43     dlvsym(ptr, "OHOS_EXTENSION_GetExtensionModule", dso_easy_symver_version_stable);
44     napi_value result = nullptr;
45     napi_create_int32(env, errno, &result);
46     return result;
47 }
48 
49 EXTERN_C_START
Init(napi_env env, napi_value exports)50 static napi_value Init(napi_env env, napi_value exports)
51 {
52     napi_property_descriptor desc[] = {
53         {"dlsym", nullptr, DlSym, nullptr, nullptr, nullptr, napi_default, nullptr},
54         {"dlvsym", nullptr, DlVSym, nullptr, nullptr, nullptr, napi_default, nullptr},
55     };
56     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
57     return exports;
58 }
59 EXTERN_C_END
60 
61 static napi_module demoModule = {
62     .nm_version = 1,
63     .nm_flags = 0,
64     .nm_filename = nullptr,
65     .nm_register_func = Init,
66     .nm_modname = "libdlfcnndk",
67     .nm_priv = ((void *)0),
68     .reserved = {0},
69 };
70 
RegisterModule(void)71 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
72