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 firstParam 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 <arpa/inet.h>
18 #include <cerrno>
19 #include <clocale>
20 #include <cmath>
21 #include <csignal>
22 #include <cstdio>
23 #include <cstring>
24 #include <dlfcn.h>
25 #include <fcntl.h>
26 #include <info/application_target_sdk_version.h>
27 #include <info/fatal_message.h>
28 #include <js_native_api_types.h>
29 #include <libgen.h>
30 #include <linux/quota.h>
31 #include <mntent.h>
32 #include <netdb.h>
33 #include <netinet/ether.h>
34 #include <pthread.h>
35 #include <regex.h>
36 #include <resolv.h>
37 #include <sched.h>
38 #include <sys/epoll.h>
39 #include <sys/eventfd.h>
40 #include <sys/file.h>
41 #include <sys/socket.h>
42 #include <sys/stat.h>
43 #include <sys/xattr.h>
44 #include <syslog.h>
45 #include <threads.h>
46 #include <unistd.h>
47 #include <utmp.h>
48
49 #define NAMELEN 16
50 #define NSEC_PER_SEC 1000000000
51 #define NSEC_PER_100MS 100000000
52 #define NSEC_PER_MSEC 1000000
53 #define US_PER_S 1000000
54 #define MS_PER_S 1000
55 #define SLEEP_100_MS 100
56 #define DELAY_TIME_100_MS 100
57 #define SLEEP_50_MS 50
58 #define SLEEP_20_MS 20
59 #define MAXDNAME_A 1025
60 #define INIT (-1)
61 #define SUCCESS 0
62 #define NO_ERROR 0
63 #define PARAM_0 0
64 #define PARAM_1 1
65 #define PARAM_5 5
66 #define PARAM_2 2
67 #define PARAM_32 32
68 #define PARAM_64 64
69 #define PARAM_10 10
70 #define PARAM_100 100
71 #define PARAM_256 256
72 #define PARAM_0777 0777
73 #define PARAM_64 64
74 #define PARAM_1024 1024
75 #define PARAM_8192 8192
76 #define SPNUM 1
77 #define MINUSONE (-1)
78 #define MINUSTWO (-2)
79 #define FAILD (-1)
80 #define PARAM_UNNORMAL (-1)
81 #define SIZE_10 10
82 #define TEST_MODE 0666
83 #define BUF_SIZE (100)
84 #define PORT 2288
85 #define PORT_2 2289
86 #ifndef tls_mod_off_t
87 #define tls_mod_off_t size_t
88 #define PARAM_72 72
89 #endif
90
91 extern "C" int __fstatat_time64(int, const char *__restrict, struct stat *__restrict, int);
92 extern "C" int __aeabi_atexit(void *obj, void (*func)(void *), void *d);
93 extern "C" uintptr_t __gnu_Unwind_Find_exidx(uintptr_t pc, int *pcount);
94
FStatAt_time64(napi_env env, napi_callback_info info)95 static napi_value FStatAt_time64(napi_env env, napi_callback_info info)
96 {
97 size_t argc = PARAM_1;
98 napi_value args[1] = {nullptr};
99 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
100 size_t length = PARAM_256;
101 size_t strResult = PARAM_0;
102 char ptr[length];
103 napi_get_value_string_utf8(env, args[0], ptr, length, &strResult);
104 int backParam = PARAM_UNNORMAL;
105 struct stat st;
106 int fd = open(ptr, O_RDWR | O_CREAT, TEST_MODE);
107 lseek(fd, PARAM_0, SEEK_SET);
108 backParam = __fstatat_time64(AT_FDCWD, ptr, &st, PARAM_0);
109 close(fd);
110 napi_value result = nullptr;
111 napi_create_int32(env, backParam, &result);
112 return result;
113 }
114
115
exitFunc(void *arg)116 void exitFunc(void *arg){};
Aeabiatexit(napi_env env, napi_callback_info info)117 static napi_value Aeabiatexit(napi_env env, napi_callback_info info)
118 {
119 int32_t var = PARAM_0;
120 int backInfo = __aeabi_atexit(&var, exitFunc, nullptr);
121 napi_value result = nullptr;
122 napi_create_int32(env, backInfo, &result);
123 return result;
124 }
125
Gnuunwindfindexidx(napi_env env, napi_callback_info info)126 static napi_value Gnuunwindfindexidx(napi_env env, napi_callback_info info)
127 {
128 uintptr_t pc = PARAM_10;
129 int32_t pcount = PARAM_32;
130 int32_t backInfo = __gnu_Unwind_Find_exidx(pc, &pcount);
131 napi_value result = nullptr;
132 napi_create_int32(env, backInfo, &result);
133 return result;
134 }
135
136 EXTERN_C_START
Init(napi_env env, napi_value exports)137 static napi_value Init(napi_env env, napi_value exports)
138 {
139 napi_property_descriptor desc[] = {
140 {"__fstatat_time64", nullptr, FStatAt_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
141 {"aeabiatexit", nullptr, Aeabiatexit, nullptr, nullptr, nullptr, napi_default, nullptr},
142 {"gnuunwindfindexidx", nullptr, Gnuunwindfindexidx, nullptr, nullptr, nullptr, napi_default, nullptr},
143
144 };
145 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
146 return exports;
147 }
148 EXTERN_C_END
149
150 static napi_module demoModule = {
151 .nm_version = 1,
152 .nm_flags = 0,
153 .nm_filename = nullptr,
154 .nm_register_func = Init,
155 .nm_modname = "libotherstestndk",
156 .nm_priv = ((void *)0),
157 .reserved = {0},
158 };
159
RegisterModule(void)160 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
161