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 <cstdlib>
18 #include <cstring>
19 #include <ctime>
20 #include <dlfcn.h>
21 #include <fnmatch.h>
22 #include <js_native_api_types.h>
23 #include <poll.h>
24 #include <pthread.h>
25 #include <semaphore.h>
26 #include <sys/resource.h>
27 #include <sys/sem.h>
28 #include <sys/socket.h>
29 #include <sys/stat.h>
30 #include <sys/timerfd.h>
31 #include <sys/timex.h>
32 #include <sys/wait.h>
33 #include <threads.h>
34 #include <unistd.h>
35 #include <utime.h>
36 
37 #define PARAM_5 5
38 #define PARAM_0 0
39 #define PARAM_1 1
40 #define PARAM_UNNORMAL (-1)
41 
42 extern "C" int __getitimer_time64(int, struct itimerval *);
GetITimer_time64(napi_env env, napi_callback_info info)43 static napi_value GetITimer_time64(napi_env env, napi_callback_info info)
44 {
45     size_t argc = PARAM_1;
46     napi_value args[1] = {nullptr};
47     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
48     int param;
49     napi_get_value_int32(env, args[0], &param);
50 
51     int ret;
52     struct itimerval val;
53     ret = __getitimer_time64(param, &val);
54 
55     napi_value result = nullptr;
56     napi_create_int32(env, ret, &result);
57     return result;
58 }
59 
60 extern "C" int __getrusage_time64(int, struct rusage *);
GetRusAge_time64(napi_env env, napi_callback_info info)61 static napi_value GetRusAge_time64(napi_env env, napi_callback_info info)
62 {
63     size_t argc = PARAM_1;
64     napi_value args[1] = {nullptr};
65     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
66     int param;
67     napi_get_value_int32(env, args[0], &param);
68 
69     int ret;
70     struct rusage val;
71     ret = __getrusage_time64(param, &val);
72 
73     napi_value result = nullptr;
74     napi_create_int32(env, ret, &result);
75     return result;
76 }
77 
78 extern "C" int __gettimeofday_time64(struct timeval *__restrict, void *__restrict);
GetTimeOfDay_time64(napi_env env, napi_callback_info info)79 static napi_value GetTimeOfDay_time64(napi_env env, napi_callback_info info)
80 {
81     size_t argc = PARAM_1;
82     napi_value args[1] = {nullptr};
83     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
84     int param;
85     napi_get_value_int32(env, args[0], &param);
86 
87     int ret;
88     struct timeval val;
89     if (param == PARAM_0) {
90         ret = __gettimeofday_time64(&val, nullptr);
91     } else {
92         struct timezone tz;
93         tz.tz_dsttime = PARAM_UNNORMAL;
94         tz.tz_minuteswest = PARAM_UNNORMAL;
95         ret = __gettimeofday_time64(&val, &tz);
96         if (tz.tz_dsttime == PARAM_UNNORMAL || tz.tz_minuteswest == PARAM_UNNORMAL) {
97             ret = PARAM_UNNORMAL;
98         }
99     }
100 
101     napi_value result = nullptr;
102     napi_create_int32(env, ret, &result);
103     return result;
104 }
105 
106 extern "C" int __pselect_time64(int, fd_set *__restrict, fd_set *__restrict, fd_set *__restrict,
107                                 const struct timespec *__restrict, const sigset_t *__restrict);
PSelect_time64(napi_env env, napi_callback_info info)108 static napi_value PSelect_time64(napi_env env, napi_callback_info info)
109 {
110     size_t argc = PARAM_1;
111     napi_value args[1] = {nullptr};
112     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
113     int param;
114     napi_get_value_int32(env, args[0], &param);
115 
116     int ret;
117     fd_set set;
118     struct timespec val;
119     FD_ZERO(&set);
120     FD_SET(STDIN_FILENO, &set);
121     val.tv_sec = PARAM_1;
122     val.tv_nsec = PARAM_0;
123     ret = __pselect_time64(param, &set, nullptr, nullptr, &val, nullptr);
124     if (ret > PARAM_0) {
125         ret = PARAM_0;
126     }
127 
128     napi_value result = nullptr;
129     napi_create_int32(env, ret, &result);
130     return result;
131 }
132 
133 extern "C" int __setitimer_time64(int, const struct itimerval *__restrict, struct itimerval *__restrict);
134 static int g_count = PARAM_0;
SignalHandler(int signo)135 void SignalHandler(int signo)
136 {
137     switch (signo) {
138     case SIGALRM:
139         struct itimerval val;
140         g_count++;
141         if (g_count > PARAM_1) {
142             __getitimer_time64(signo, &val);
143             val.it_value.tv_sec = PARAM_0;
144             val.it_value.tv_usec = PARAM_0;
145             __setitimer_time64(signo, &val, nullptr);
146             g_count = PARAM_0;
147         }
148         break;
149     default:
150         break;
151     }
152 }
SetITimer_time64(napi_env env, napi_callback_info info)153 static napi_value SetITimer_time64(napi_env env, napi_callback_info info)
154 {
155     signal(SIGALRM, SignalHandler);
156     size_t argc = PARAM_1;
157     napi_value args[1] = {nullptr};
158     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
159     int param;
160     napi_get_value_int32(env, args[0], &param);
161 
162     int ret;
163     struct itimerval val;
164     __getitimer_time64(param, &val);
165     ret = __setitimer_time64(param, &val, nullptr);
166 
167     napi_value result = nullptr;
168     napi_create_int32(env, ret, &result);
169     return result;
170 }
171 
172 EXTERN_C_START
Init(napi_env env, napi_value exports)173 static napi_value Init(napi_env env, napi_value exports)
174 {
175     napi_property_descriptor desc[] = {
176         {"__getitimer_time64", nullptr, GetITimer_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
177         {"__getrusage_time64", nullptr, GetRusAge_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
178         {"__gettimeofday_time64", nullptr, GetTimeOfDay_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
179         {"__pselect_time64", nullptr, PSelect_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
180         {"__setitimer_time64", nullptr, SetITimer_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
181     };
182     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
183     return exports;
184 }
185 EXTERN_C_END
186 
187 static napi_module demoModule = {
188     .nm_version = 1,
189     .nm_flags = 0,
190     .nm_filename = "fnmatch1ndk",
191     .nm_register_func = Init,
192     .nm_modname = "libfnmatch1ndk",
193     .nm_priv = ((void *)0),
194     .reserved = {0},
195 };
196 
RegisterModule(void)197 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
198