• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 <elf.h>
18 #include <ifaddrs.h>
19 #include <js_native_api_types.h>
20 #include <net/if.h>
21 #include <sys/auxv.h>
22 #include <sys/inotify.h>
23 #include <utmp.h>
24 #include <uv.h>
25 
26 #define NO_ERR 0
27 #define SUCCESS 1
28 #define FAIL (-1)
29 #define PARAM_0 0
30 #define PARAM_UNNORMAL (-1)
31 #define ERRNO_0 0
32 #define ONEVAL 1
33 
Getauxval(napi_env env,napi_callback_info info)34 static napi_value Getauxval(napi_env env, napi_callback_info info)
35 {
36     size_t argc = ONEVAL;
37     napi_value args[1] = {nullptr};
38     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
39     int param;
40     napi_get_value_int32(env, args[0], &param);
41 
42     unsigned long int val;
43     errno = ERRNO_0;
44     if (param == PARAM_0) {
45         val = getauxval(AT_BASE);
46     } else {
47         val = getauxval(PARAM_UNNORMAL);
48     }
49     int ret;
50     if (val == PARAM_0) {
51         ret = FAIL;
52     } else {
53         ret = SUCCESS;
54     }
55 
56     napi_value result = nullptr;
57     napi_create_int32(env, ret, &result);
58     return result;
59 }
60 
61 EXTERN_C_START
Init(napi_env env,napi_value exports)62 static napi_value Init(napi_env env, napi_value exports)
63 {
64     napi_property_descriptor desc[] = {
65         {"getauxval", nullptr, Getauxval, nullptr, nullptr, nullptr, napi_default, nullptr},
66     };
67     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
68     return exports;
69 }
70 EXTERN_C_END
71 
72 static napi_module demoModule = {
73     .nm_version = 1,
74     .nm_flags = 0,
75     .nm_filename = nullptr,
76     .nm_register_func = Init,
77     .nm_modname = "auxv1ndk",
78     .nm_priv = ((void *)0),
79     .reserved = {0},
80 };
81 
RegisterModule(void)82 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
83