• 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 <cerrno>
18 #include <cstdio>
19 #include <cstdlib>
20 #include <ctime>
21 #include <dlfcn.h>
22 #include <fcntl.h>
23 #include <fnmatch.h>
24 #include <js_native_api_types.h>
25 #include <netinet/in.h>
26 #include <poll.h>
27 #include <pthread.h>
28 #include <semaphore.h>
29 #include <sys/resource.h>
30 #include <sys/sem.h>
31 #include <sys/socket.h>
32 #include <sys/stat.h>
33 #include <sys/timerfd.h>
34 #include <sys/timex.h>
35 #include <sys/wait.h>
36 #include <threads.h>
37 #include <unistd.h>
38 #include <utime.h>
39 
40 #define PARAM_5 5
41 #define PARAM_0 0
42 #define PARAM_1 1
43 #define PARAM_UNNORMAL (-1)
44 #define PARAM_1999 1999
45 #define PARAM_12 12
46 #define PARAM_17 17
47 #define PARAM_06 06
48 #define PARAM_30 30
49 #define PARAM_23 23
50 #define PARAM_1900 1900
51 #define PARAM_02 02
52 #define PARAM_07 07
53 #define PARAM_16 16
54 #define PARAM_15 15
55 #define PARAM_20 20
56 #define SUCCESS 1
57 #define SIZE_64 64
58 #define SIZE_10 10
59 #define SEC_TIME 123840
60 #define FAIL (-1)
61 #define MPARAM_1 (-1)
62 #define TEST_SIZE 2
63 #define MICROSECONDS (1000000)
64 #define TEST_FILE "/data/storage/el2/base/files/test.txt"
65 #define PARAM_0777 0777
66 #define PATH "/data/storage/el2/base/files/utime64.txt"
67 #define TEST_FILE_NAME "test.txt"
68 #define TEST_FILE_PATH "/data/storage/el2/base/files/"
69 #define PARAM_0666 0666
70 
FnMatch(napi_env env,napi_callback_info info)71 static napi_value FnMatch(napi_env env, napi_callback_info info)
72 {
73     char firstStr[] = "video_???_test.txt";
74     char secondStr[] = "video_010_test.txt";
75     int backParam = fnmatch(firstStr, secondStr, FNM_NOESCAPE);
76     napi_value result = nullptr;
77     napi_create_int32(env, backParam, &result);
78     return result;
79 }
80 
81 extern "C" int __clock_gettime64(clockid_t, struct timespec *);
clock_getTime64(napi_env env,napi_callback_info info)82 static napi_value clock_getTime64(napi_env env, napi_callback_info info)
83 {
84     int returnParam = PARAM_1;
85     struct timespec timeSpec {};
86     returnParam = __clock_gettime64(CLOCK_REALTIME, &timeSpec);
87     napi_value result = nullptr;
88     napi_create_int32(env, returnParam, &result);
89     return result;
90 }
91 
92 extern "C" int __pthread_cond_timedwait_time64(pthread_cond_t *__restrict, pthread_mutex_t *__restrict,
93                                                const struct timespec *__restrict);
PThread_cond_timedWait_time64(napi_env env,napi_callback_info info)94 static napi_value PThread_cond_timedWait_time64(napi_env env, napi_callback_info info)
95 {
96     int returnParam = PARAM_1;
97     pthread_cond_t pCond = PTHREAD_COND_INITIALIZER;
98     pthread_mutex_t pMutex = PTHREAD_MUTEX_INITIALIZER;
99     const struct timespec timeSpec {};
100     returnParam = __pthread_cond_timedwait_time64(&pCond, &pMutex, &timeSpec);
101     napi_value result = nullptr;
102     napi_create_int32(env, returnParam, &result);
103     return result;
104 }
105 
106 extern "C" int __nanosleep_time64(const struct timespec *, struct timespec *);
NanoSleep_time64(napi_env env,napi_callback_info info)107 static napi_value NanoSleep_time64(napi_env env, napi_callback_info info)
108 {
109     int returnParam = PARAM_1;
110     struct timespec timeSpec {
111         PARAM_0, PARAM_0
112     };
113     timeSpec.tv_sec = PARAM_5;
114     timeSpec.tv_nsec = PARAM_0;
115     returnParam = __nanosleep_time64(&timeSpec, &timeSpec);
116     napi_value result = nullptr;
117     napi_create_int32(env, returnParam, &result);
118     return result;
119 }
120 
121 extern "C" int __fstat_time64(int, struct stat *);
FStat_time64(napi_env env,napi_callback_info info)122 static napi_value FStat_time64(napi_env env, napi_callback_info info)
123 {
124     int returnParam = PARAM_1;
125     int fileDescribe = open("/data/storage/el2/base/files/Fzl.txt", O_CREAT);
126     struct stat stat {};
127     returnParam = __fstat_time64(fileDescribe, &stat);
128     napi_value result = nullptr;
129     close(fileDescribe);
130     napi_create_int32(env, returnParam, &result);
131     return result;
132 }
133 
134 extern "C" int __lstat_time64(const char *__restrict, struct stat *__restrict);
LStat_time64(napi_env env,napi_callback_info info)135 static napi_value LStat_time64(napi_env env, napi_callback_info info)
136 {
137     int returnParam = PARAM_1;
138     const char *test = "/data/storage/el2/base/files/Fzl.txt";
139     struct stat stat {};
140     returnParam = __lstat_time64(test, &stat);
141     napi_value result = nullptr;
142     napi_create_int32(env, returnParam, &result);
143     return result;
144 }
145 
146 extern "C" int __stat_time64(const char *__restrict, struct stat *__restrict);
Stat_time64(napi_env env,napi_callback_info info)147 static napi_value Stat_time64(napi_env env, napi_callback_info info)
148 {
149     int returnParam = PARAM_1;
150     const char *firstParam = "/data/storage/el2/base/files/Fzl.txt";
151     struct stat stat {};
152     returnParam = __stat_time64(firstParam, &stat);
153     napi_value result = nullptr;
154     napi_create_int32(env, returnParam, &result);
155     return result;
156 }
157 
158 extern "C" int __utimensat_time64(int, const char *, const struct timespec[2], int);
UTimeNsAt_time64(napi_env env,napi_callback_info info)159 static napi_value UTimeNsAt_time64(napi_env env, napi_callback_info info)
160 {
161     size_t argc = PARAM_1;
162     napi_value args[1] = {nullptr};
163     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
164     int toCppResult = FAIL;
165     size_t length = SIZE_64, stresult = PARAM_0;
166     char path[SIZE_64] = {PARAM_0};
167     napi_get_value_string_utf8(env, args[0], path, length, &stresult);
168     int fileDescribe = open(path, O_CREAT);
169     const long sec = SEC_TIME;
170     struct timespec times[] = {{.tv_sec = PARAM_0}, {.tv_sec = sec}};
171     int utimensatValue = __utimensat_time64(fileDescribe, path, times, PARAM_0);
172     close(fileDescribe);
173     struct stat statbuf = {PARAM_0};
174     utimensatValue = stat(path, &statbuf);
175     if (utimensatValue == PARAM_0 && statbuf.st_mtim.tv_sec == sec) {
176         toCppResult = SUCCESS;
177     }
178     remove(path);
179     napi_value result = nullptr;
180     napi_create_int32(env, toCppResult, &result);
181     return result;
182 }
183 
184 extern "C" int __clock_getres_time64(clockid_t, struct timespec *);
Clock_getRes_time64(napi_env env,napi_callback_info info)185 static napi_value Clock_getRes_time64(napi_env env, napi_callback_info info)
186 {
187     int returnParam = PARAM_1;
188     clockid_t firstParam = PARAM_1;
189     struct timespec secondParam {};
190     returnParam = __clock_getres_time64(firstParam, &secondParam);
191     napi_value result = nullptr;
192     napi_create_int32(env, returnParam, &result);
193     return result;
194 }
195 
196 extern "C" int __clock_nanosleep_time64(clockid_t, int, const struct timespec *, struct timespec *);
Clock_nanoSleep_time64(napi_env env,napi_callback_info info)197 static napi_value Clock_nanoSleep_time64(napi_env env, napi_callback_info info)
198 {
199     int returnParam = PARAM_1, secondParam = PARAM_1;
200     clockid_t firstParam = PARAM_1;
201     const struct timespec thirdParam {};
202     struct timespec fourthParam {};
203     returnParam = __clock_nanosleep_time64(firstParam, secondParam, &thirdParam, &fourthParam);
204     napi_value result = nullptr;
205     napi_create_int32(env, returnParam, &result);
206     return result;
207 }
208 
209 extern "C" int __clock_settime64(clockid_t, const struct timespec *);
Clock_setTime64(napi_env env,napi_callback_info info)210 static napi_value Clock_setTime64(napi_env env, napi_callback_info info)
211 {
212     int returnParam = PARAM_1;
213     struct timespec firstParam {};
214     clock_gettime(CLOCK_REALTIME, &firstParam);
215     returnParam = __clock_settime64(CLOCK_REALTIME, &firstParam);
216     napi_value result = nullptr;
217     napi_create_int32(env, returnParam, &result);
218     return result;
219 }
220 
221 extern "C" int __cnd_timedwait_time64(cnd_t *__restrict, mtx_t *__restrict, const struct timespec *__restrict);
Cnd_timedWait_time64(napi_env env,napi_callback_info info)222 static napi_value Cnd_timedWait_time64(napi_env env, napi_callback_info info)
223 {
224     int returnParam = PARAM_1;
225     cnd_t firstParam;
226     mtx_t secondParam;
227     cnd_init(&firstParam);
228     mtx_lock(&secondParam);
229     struct timespec thirdParam {};
230     returnParam = __cnd_timedwait_time64(&firstParam, &secondParam, &thirdParam);
231     napi_value result = nullptr;
232     napi_create_int32(env, returnParam, &result);
233     return result;
234 }
235 
236 extern "C" char *__ctime64(const time_t *);
CTime64(napi_env env,napi_callback_info info)237 static napi_value CTime64(napi_env env, napi_callback_info info)
238 {
239     char *checkParam = nullptr;
240     int returnParam = PARAM_1;
241     const time_t time = PARAM_0;
242     checkParam = __ctime64(&time);
243     if (checkParam != nullptr) {
244         returnParam = PARAM_0;
245     }
246     napi_value result = nullptr;
247     napi_create_int32(env, returnParam, &result);
248     return result;
249 }
250 
251 extern "C" char *__ctime64_r(const time_t *, char *);
CTime64_r(napi_env env,napi_callback_info info)252 static napi_value CTime64_r(napi_env env, napi_callback_info info)
253 {
254     char *checkParam = nullptr;
255     int returnParam = PARAM_1;
256     const time_t time = PARAM_0;
257     char secondParam[32];
258     checkParam = __ctime64_r(&time, secondParam);
259     if (checkParam != nullptr) {
260         returnParam = PARAM_0;
261     }
262     napi_value result = nullptr;
263     napi_create_int32(env, returnParam, &result);
264     return result;
265 }
266 
267 extern "C" double __difftime64(time_t, time_t);
DiffTime64(napi_env env,napi_callback_info info)268 static napi_value DiffTime64(napi_env env, napi_callback_info info)
269 {
270     double checkParam;
271     int returnParam = PARAM_1;
272     time_t now;
273     struct tm newyear;
274     time(&now);
275     newyear = *localtime(&now);
276     newyear.tm_hour = PARAM_0;
277     newyear.tm_min = PARAM_0;
278     newyear.tm_sec = PARAM_0;
279     newyear.tm_mon = PARAM_0;
280     newyear.tm_mday = PARAM_1;
281     time_t firstTime = PARAM_1999 - PARAM_12 - PARAM_17 - PARAM_06 - PARAM_30 - PARAM_23;
282     time_t secondTime = PARAM_1900 - PARAM_02 - PARAM_07 - PARAM_16 - PARAM_15 - PARAM_20;
283     checkParam = __difftime64(firstTime, secondTime);
284     if (checkParam) {
285         returnParam = PARAM_0;
286     }
287     napi_value result = nullptr;
288     napi_create_double(env, returnParam, &result);
289     return result;
290 }
291 
292 extern "C" void *__dlsym_time64(void *__restrict, const char *__restrict);
DlSym_time64(napi_env env,napi_callback_info info)293 static napi_value DlSym_time64(napi_env env, napi_callback_info info)
294 {
295     const char *path = "/system/lib/extensionability/libstatic_subscriber_extension_module.z.so";
296     void *ptr = dlopen(path, RTLD_LAZY);
297     errno = PARAM_0;
298     __dlsym_time64(ptr, "OHOS_EXTENSION_GetExtensionModule");
299     napi_value result = nullptr;
300     napi_create_int32(env, errno, &result);
301     return result;
302 }
303 
304 extern "C" int __futimens_time64(int, const struct timespec[2]);
FuTimeNs_time64(napi_env env,napi_callback_info info)305 static napi_value FuTimeNs_time64(napi_env env, napi_callback_info info)
306 {
307     int fileDescribe = open(PATH, O_CREAT);
308     int ret = __futimens_time64(fileDescribe, ((struct timespec[2]){{.tv_nsec = UTIME_OMIT}, {.tv_nsec = UTIME_OMIT}}));
309     napi_value result;
310     napi_create_int32(env, ret, &result);
311     close(fileDescribe);
312     remove(PATH);
313     return result;
314 }
315 
316 extern "C" int __futimes_time64(int, const struct timeval[2]);
FuTimes_time64(napi_env env,napi_callback_info info)317 static napi_value FuTimes_time64(napi_env env, napi_callback_info info)
318 {
319     struct stat s;
320     static struct timeval tv[2];
321     tv[0].tv_sec = s.st_atime;
322     tv[0].tv_usec = PARAM_0;
323     tv[1].tv_sec = s.st_mtime;
324     tv[1].tv_usec = PARAM_0;
325     int ret = MPARAM_1;
326     int fileDescribe = open(TEST_FILE, O_RDWR | O_CREAT, PARAM_0777);
327     if (fileDescribe != MPARAM_1) {
328         ret = __futimes_time64(fileDescribe, tv);
329     }
330     napi_value result = nullptr;
331     napi_create_int32(env, ret, &result);
332     remove(TEST_FILE);
333     return result;
334 }
335 
336 extern "C" int __futimesat_time64(int, const char *, const struct timeval[2]);
FuTimesAt_time64(napi_env env,napi_callback_info info)337 static napi_value FuTimesAt_time64(napi_env env, napi_callback_info info)
338 {
339     int dir_fd = open(TEST_FILE_PATH, O_RDONLY | O_DIRECTORY);
340     int fileDescribe = openat(dir_fd, TEST_FILE_NAME, O_CREAT | O_RDWR | O_EXCL, PARAM_0666);
341     const char *msg = "helloworld";
342     write(fileDescribe, "msg", sizeof(msg));
343     struct timeval tv[2];
344     struct stat st;
345     fstat(fileDescribe, &st);
346     close(fileDescribe);
347     tv[0].tv_sec = st.st_atime + PARAM_1;
348     tv[0].tv_usec = PARAM_0;
349     tv[1].tv_sec = st.st_mtime + PARAM_1;
350     tv[1].tv_usec = PARAM_0;
351     int ret = __futimesat_time64(dir_fd, TEST_FILE_NAME, tv);
352     unlinkat(dir_fd, TEST_FILE_NAME, PARAM_0);
353     close(dir_fd);
354     remove(TEST_FILE);
355     napi_value result;
356     napi_create_int32(env, ret, &result);
357     return result;
358 }
359 
360 extern "C" struct tm *__gmtime64(const time_t *);
GmTime64(napi_env env,napi_callback_info info)361 static napi_value GmTime64(napi_env env, napi_callback_info info)
362 {
363     int backParam = PARAM_UNNORMAL;
364     struct tm *checkParam{};
365     const time_t firstParam = PARAM_0;
366     checkParam = __gmtime64(&firstParam);
367     if (checkParam != nullptr) {
368         backParam = PARAM_0;
369     }
370     napi_value result = nullptr;
371     napi_create_int32(env, backParam, &result);
372     return result;
373 }
374 
375 extern "C" struct tm *__gmtime64_r(const time_t *__restrict, struct tm *__restrict);
GmTime64_r(napi_env env,napi_callback_info info)376 static napi_value GmTime64_r(napi_env env, napi_callback_info info)
377 {
378     int backParam = PARAM_UNNORMAL;
379     struct tm *checkParam{};
380     const time_t firstParam = PARAM_0;
381     struct tm secondParam {};
382     checkParam = __gmtime64_r(&firstParam, &secondParam);
383     if (checkParam != nullptr) {
384         backParam = PARAM_0;
385     }
386     napi_value result = nullptr;
387     napi_create_int32(env, backParam, &result);
388     return result;
389 }
390 
391 extern "C" struct tm *__localtime64(const time_t *);
Localtime64(napi_env env,napi_callback_info info)392 static napi_value Localtime64(napi_env env, napi_callback_info info)
393 {
394     int backParam = PARAM_UNNORMAL;
395     struct tm *checkParam{};
396     const time_t firstParam = PARAM_0;
397     checkParam = __localtime64(&firstParam);
398     if (checkParam != nullptr) {
399         backParam = PARAM_0;
400     }
401     napi_value result = nullptr;
402     napi_create_int32(env, backParam, &result);
403     return result;
404 }
405 
406 extern "C" struct tm *__localtime64_r(const time_t *__restrict, struct tm *__restrict);
Localtime64_r(napi_env env,napi_callback_info info)407 static napi_value Localtime64_r(napi_env env, napi_callback_info info)
408 {
409     int backParam = PARAM_UNNORMAL;
410     struct tm *checkParam{};
411     const time_t firstParam = PARAM_0;
412     struct tm secondParam {};
413     checkParam = __localtime64_r(&firstParam, &secondParam);
414     if (checkParam != nullptr) {
415         backParam = PARAM_0;
416     }
417     napi_value result = nullptr;
418     napi_create_int32(env, backParam, &result);
419     return result;
420 }
421 
422 extern "C" int __lutimes_time64(const char *, const struct timeval[2]);
LuTimes_time64(napi_env env,napi_callback_info info)423 static napi_value LuTimes_time64(napi_env env, napi_callback_info info)
424 {
425     int ret;
426     struct stat s;
427     static struct timeval tv[TEST_SIZE] = {{0L, 0L}, {0L, 0L}};
428     tv[0].tv_sec = s.st_atime;
429     tv[0].tv_usec = PARAM_0;
430     tv[1].tv_sec = s.st_mtime;
431     tv[1].tv_usec = PARAM_0;
432     int fd = open("/data/storage/el2/base/files/Fzl.txt", O_RDWR | O_CREAT, PARAM_0666);
433     close(fd);
434     ret = __lutimes_time64("/data/storage/el2/base/files/Fzl.txt", tv);
435     napi_value result;
436     if (ret == FAIL) {
437         napi_create_int32(env, FAIL, &result);
438     } else {
439         napi_create_int32(env, PARAM_0, &result);
440     }
441     return result;
442 }
443 
444 extern "C" time_t __mktime64(struct tm *);
MkTime64(napi_env env,napi_callback_info info)445 static napi_value MkTime64(napi_env env, napi_callback_info info)
446 {
447     time_t checkParam = PARAM_UNNORMAL;
448     int backParam = PARAM_1;
449     struct tm firstParam {};
450     checkParam = __mktime64(&firstParam);
451     if (checkParam != PARAM_UNNORMAL) {
452         backParam = PARAM_0;
453     }
454     napi_value result = nullptr;
455     napi_create_int32(env, backParam, &result);
456     return result;
457 }
458 
459 extern "C" int __mtx_timedlock_time64(mtx_t *__restrict, const struct timespec *__restrict);
Mtx_timedLock_time64(napi_env env,napi_callback_info info)460 static napi_value Mtx_timedLock_time64(napi_env env, napi_callback_info info)
461 {
462     int backParam = PARAM_UNNORMAL;
463     mtx_t firstParam;
464     mtx_init(&firstParam, mtx_timed);
465     struct timespec secondParam;
466     secondParam.tv_sec = PARAM_02;
467     secondParam.tv_nsec = PARAM_0;
468     backParam = __mtx_timedlock_time64(&firstParam, &secondParam);
469     mtx_unlock(&firstParam);
470     napi_value result = nullptr;
471     napi_create_int32(env, backParam, &result);
472     return result;
473 }
474 
475 extern "C" int __ppoll_time64(struct pollfd *, nfds_t, const struct timespec *, const sigset_t *);
PPoll_time64(napi_env env,napi_callback_info info)476 static napi_value PPoll_time64(napi_env env, napi_callback_info info)
477 {
478     int backParam = PARAM_UNNORMAL;
479     struct pollfd *firstParam{};
480     nfds_t secondParam = PARAM_0;
481     struct timespec thirdParam {};
482     const sigset_t *fourthParam = nullptr;
483     backParam = __ppoll_time64(firstParam, secondParam, &thirdParam, fourthParam);
484     napi_value result = nullptr;
485     napi_create_int32(env, backParam, &result);
486     return result;
487 }
488 
489 extern "C" int __pthread_mutex_timedlock_time64(pthread_mutex_t *__restrict, const struct timespec *__restrict);
PThread_mutex_timedLock_time64(napi_env env,napi_callback_info info)490 static napi_value PThread_mutex_timedLock_time64(napi_env env, napi_callback_info info)
491 {
492     int backParam = PARAM_UNNORMAL;
493     pthread_mutex_t firstParam = PTHREAD_MUTEX_INITIALIZER;
494     struct timespec secondParam {};
495     backParam = pthread_mutex_timedlock(&firstParam, &secondParam);
496     napi_value result = nullptr;
497     napi_create_int32(env, backParam, &result);
498     return result;
499 }
500 
501 extern "C" int __pthread_rwlock_timedrdlock_time64(pthread_rwlock_t *__restrict, const struct timespec *__restrict);
PThread_rwLock_timedrdLock_time64(napi_env env,napi_callback_info info)502 static napi_value PThread_rwLock_timedrdLock_time64(napi_env env, napi_callback_info info)
503 {
504     int backParam = PARAM_UNNORMAL;
505     pthread_rwlock_t firstParam = PTHREAD_RWLOCK_INITIALIZER;
506     struct timespec secondParam {};
507     backParam = __pthread_rwlock_timedrdlock_time64(&firstParam, &secondParam);
508     napi_value result = nullptr;
509     napi_create_int32(env, backParam, &result);
510     return result;
511 }
512 
513 extern "C" int __pthread_rwlock_timedwrlock_time64(pthread_rwlock_t *__restrict, const struct timespec *__restrict);
PThread_rwLock_timedWrLock_time64(napi_env env,napi_callback_info info)514 static napi_value PThread_rwLock_timedWrLock_time64(napi_env env, napi_callback_info info)
515 {
516     int backParam = PARAM_UNNORMAL;
517     pthread_rwlock_t firstParam = PTHREAD_RWLOCK_INITIALIZER;
518     const struct timespec *secondParam{};
519     backParam = __pthread_rwlock_timedwrlock_time64(&firstParam, secondParam);
520     napi_value result = nullptr;
521     napi_create_int32(env, backParam, &result);
522     return result;
523 }
524 
525 extern "C" int __recvmmsg_time64(int, struct mmsghdr *, unsigned int, unsigned int, struct timespec *);
ReCvmMsg_time64(napi_env env,napi_callback_info info)526 static napi_value ReCvmMsg_time64(napi_env env, napi_callback_info info)
527 {
528 #define BUFSIZE 200
529     int resultValue = FAIL;
530     pid_t pid = fork();
531     if (pid > PARAM_0) {
532         struct mmsghdr msgs[SIZE_10];
533         struct iovec iovecs[SIZE_10];
534         char bufs[SIZE_10][BUFSIZE + PARAM_1];
535         struct sockaddr_in addr;
536         int sockfd = socket(AF_INET, SOCK_DGRAM, PARAM_0);
537         addr.sin_family = AF_INET;
538         addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
539         addr.sin_port = htons(PARAM_1999);
540         if (bind(sockfd, reinterpret_cast<sockaddr *>(static_cast<struct sockaddr_in *>(&addr)), sizeof(addr)) ==
541             FAIL) {
542             resultValue = PARAM_30;
543         } else {
544             memset(msgs, PARAM_0, sizeof(msgs));
545             for (int i = 0; i < SIZE_10; i++) {
546                 iovecs[i].iov_base = bufs[i];
547                 iovecs[i].iov_len = BUFSIZE;
548                 msgs[i].msg_hdr.msg_iov = &iovecs[i];
549                 msgs[i].msg_hdr.msg_iovlen = PARAM_1;
550             }
551             struct timespec timeout;
552             timeout.tv_sec = PARAM_1;
553             timeout.tv_nsec = PARAM_0;
554             resultValue = __recvmmsg_time64(sockfd, msgs, SIZE_10, PARAM_0, &timeout);
555         }
556         close(sockfd);
557     } else if (pid == PARAM_0) {
558         sleep(PARAM_1);
559         int sockfd;
560         struct sockaddr_in addr;
561         struct mmsghdr msg[PARAM_1];
562         struct iovec msg1[PARAM_1];
563 
564         sockfd = socket(AF_INET, SOCK_DGRAM, PARAM_0);
565 
566         addr.sin_family = AF_INET;
567         addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
568         addr.sin_port = htons(PARAM_1999);
569         connect(sockfd, reinterpret_cast<sockaddr *>(static_cast<struct sockaddr_in *>(&addr)), sizeof(addr));
570         memset(msg1, PARAM_0, sizeof(msg1));
571         memset(msg, PARAM_0, sizeof(msg));
572         msg[PARAM_0].msg_hdr.msg_iov = msg1;
573         msg[PARAM_0].msg_hdr.msg_iovlen = PARAM_1;
574 
575         sendmmsg(sockfd, msg, PARAM_1, PARAM_0);
576         close(sockfd);
577         _exit(PARAM_0);
578     }
579 
580     napi_value result = nullptr;
581     napi_create_int32(env, resultValue, &result);
582     return result;
583 }
584 
585 extern "C" int __sched_rr_get_interval_time64(pid_t, struct timespec *);
SchEd_rr_get_interval_time64(napi_env env,napi_callback_info info)586 static napi_value SchEd_rr_get_interval_time64(napi_env env, napi_callback_info info)
587 {
588     int backParam = PARAM_UNNORMAL;
589     pid_t firstParam = getpid();
590     struct timespec secondParam {};
591     backParam = __sched_rr_get_interval_time64(firstParam, &secondParam);
592     napi_value result = nullptr;
593     napi_create_int32(env, backParam, &result);
594     return result;
595 }
596 
597 extern "C" int __select_time64(int, fd_set *__restrict, fd_set *__restrict, fd_set *__restrict,
598                                struct timeval *__restrict);
Select_time64(napi_env env,napi_callback_info info)599 static napi_value Select_time64(napi_env env, napi_callback_info info)
600 {
601     int backParam = PARAM_UNNORMAL, firstParam = PARAM_1;
602     fd_set secondParam;
603     fd_set thirdParam;
604     fd_set fourthParam;
605     struct timeval fifthParam {};
606     backParam = __select_time64(firstParam, &secondParam, &thirdParam, &fourthParam, &fifthParam);
607     napi_value result = nullptr;
608     napi_create_int32(env, backParam, &result);
609     return result;
610 }
611 
612 extern "C" int __sem_timedwait_time64(sem_t *__restrict, const struct timespec *__restrict);
Sem_timedWait_time64(napi_env env,napi_callback_info info)613 static napi_value Sem_timedWait_time64(napi_env env, napi_callback_info info)
614 {
615     int backParam = PARAM_UNNORMAL;
616     sem_t firstParam;
617     struct timespec secondParam {};
618     backParam = __sem_timedwait_time64(&firstParam, &secondParam);
619     napi_value result = nullptr;
620     napi_create_int32(env, backParam, &result);
621     return result;
622 }
623 
624 extern "C" int __semtimedop_time64(int, struct sembuf *, size_t, const struct timespec *);
SemTimeDop_time64(napi_env env,napi_callback_info info)625 static napi_value SemTimeDop_time64(napi_env env, napi_callback_info info)
626 {
627     int backParam = PARAM_UNNORMAL, firstParam = PARAM_UNNORMAL;
628     struct sembuf secondParam {};
629     size_t thirdParam = 256;
630     struct timespec fourthParam {};
631     backParam = __semtimedop_time64(firstParam, &secondParam, thirdParam, &fourthParam);
632     napi_value result = nullptr;
633     napi_create_int32(env, backParam, &result);
634     return result;
635 }
636 
637 extern "C" int __settimeofday_time64(const struct timeval *, const struct timezone *);
SetTimeOfDay_time64(napi_env env,napi_callback_info info)638 static napi_value SetTimeOfDay_time64(napi_env env, napi_callback_info info)
639 {
640     int backParam = PARAM_UNNORMAL;
641     struct timeval firstParam {};
642     struct timezone secondParam {};
643     backParam = __settimeofday_time64(&firstParam, &secondParam);
644     napi_value result = nullptr;
645     napi_create_int32(env, backParam, &result);
646     return result;
647 }
648 
649 extern "C" int __sigtimedwait_time64(const sigset_t *__restrict, siginfo_t *__restrict,
650                                      const struct timespec *__restrict);
SigTimedWait_time64(napi_env env,napi_callback_info info)651 static napi_value SigTimedWait_time64(napi_env env, napi_callback_info info)
652 {
653     int backParam = PARAM_UNNORMAL;
654     const sigset_t firstParam{};
655     siginfo_t secondParam;
656     struct timespec thirdParam {};
657     backParam = __sigtimedwait_time64(&firstParam, &secondParam, &thirdParam);
658     napi_value result = nullptr;
659     napi_create_int32(env, backParam, &result);
660     return result;
661 }
662 
663 extern "C" int __thrd_sleep_time64(const struct timespec *, struct timespec *);
THrd_sleep_time64(napi_env env,napi_callback_info info)664 static napi_value THrd_sleep_time64(napi_env env, napi_callback_info info)
665 {
666     int backParam = PARAM_UNNORMAL;
667     struct timespec firstParam {};
668     struct timespec secondParam {};
669     backParam = __thrd_sleep_time64(&firstParam, &secondParam);
670     napi_value result = nullptr;
671     napi_create_int32(env, backParam, &result);
672     return result;
673 }
674 
675 extern "C" time_t __time64(time_t *);
Time64(napi_env env,napi_callback_info info)676 static napi_value Time64(napi_env env, napi_callback_info info)
677 {
678     time_t checkParam;
679     int backParam = PARAM_1;
680     time_t firstParam;
681     checkParam = __time64(&firstParam);
682     if (checkParam != PARAM_UNNORMAL) {
683         backParam = PARAM_0;
684     }
685     napi_value result = nullptr;
686     napi_create_int32(env, backParam, &result);
687     return result;
688 }
689 
690 extern "C" time_t __timegm_time64(struct tm *);
TimeGm_time64(napi_env env,napi_callback_info info)691 static napi_value TimeGm_time64(napi_env env, napi_callback_info info)
692 {
693     time_t checkParam = PARAM_0;
694     int backParam = PARAM_1;
695     struct tm firstParam {};
696     backParam = __timegm_time64(&firstParam);
697     if (checkParam != PARAM_UNNORMAL) {
698         backParam = PARAM_0;
699     }
700     napi_value result = nullptr;
701     napi_create_int32(env, backParam, &result);
702     return result;
703 }
704 
705 extern "C" int __timerfd_gettime64(int, struct itimerspec *);
TimerFd_getTime64(napi_env env,napi_callback_info info)706 static napi_value TimerFd_getTime64(napi_env env, napi_callback_info info)
707 {
708 #define NANOSECOND (1000000000)
709     int backParam = PARAM_1;
710     int ret = PARAM_1;
711     struct itimerspec its = {{PARAM_0, PARAM_0}, {PARAM_02, PARAM_0}};
712     struct itimerspec val;
713     int fd;
714     fd = timerfd_create(CLOCK_REALTIME, PARAM_0);
715     timerfd_settime(fd, PARAM_0, &its, nullptr);
716     usleep(MICROSECONDS);
717     ret = __timerfd_gettime64(fd, &val);
718     if (ret == PARAM_0) {
719         if (val.it_value.tv_nsec <= NANOSECOND) {
720             backParam = PARAM_0;
721         }
722     }
723     napi_value result = nullptr;
724     napi_create_int32(env, backParam, &result);
725     return result;
726 }
727 
728 extern "C" int __timerfd_settime64(int, int, const struct itimerspec *, struct itimerspec *);
TimerFd_setTime64(napi_env env,napi_callback_info info)729 static napi_value TimerFd_setTime64(napi_env env, napi_callback_info info)
730 {
731     struct itimerspec its = {{PARAM_0, PARAM_0}, {TEST_SIZE, PARAM_0}};
732     struct itimerspec val;
733     int fileDescribe, time_value;
734     fileDescribe = timerfd_create(CLOCK_REALTIME, PARAM_0);
735     time_value = __timerfd_settime64(fileDescribe, PARAM_0, &its, nullptr);
736     time_value = usleep(MICROSECONDS);
737     time_value = __timerfd_gettime64(fileDescribe, &val);
738     napi_value result = nullptr;
739     napi_create_int32(env, time_value, &result);
740     return result;
741 }
742 
743 extern "C" int __timespec_get_time64(struct timespec *, int);
TimerSpec_get_time64(napi_env env,napi_callback_info info)744 static napi_value TimerSpec_get_time64(napi_env env, napi_callback_info info)
745 {
746     int backParam;
747     struct timespec firstParam {};
748     backParam = __timespec_get_time64(&firstParam, TIME_UTC);
749     napi_value result = nullptr;
750     napi_create_int32(env, backParam, &result);
751     return result;
752 }
753 
754 extern "C" int __utime64(const char *, const struct utimbuf *);
UTime64(napi_env env,napi_callback_info info)755 static napi_value UTime64(napi_env env, napi_callback_info info)
756 {
757     size_t argc = PARAM_1;
758     napi_value args[PARAM_1] = {nullptr};
759     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
760     struct utimbuf ubuf;
761     struct stat statinfo;
762     int toCppResult = FAIL;
763     size_t length = SIZE_64, stresult = PARAM_0;
764     char strTemp[length];
765     napi_get_value_string_utf8(env, args[0], strTemp, length, &stresult);
766     int fileDescribe = open(strTemp, O_CREAT);
767     close(fileDescribe);
768     stat(strTemp, &statinfo);
769     ubuf.modtime = PARAM_0;
770     time(&ubuf.actime);
771     if (__utime64(strTemp, &ubuf) == PARAM_0) {
772         stat(strTemp, &statinfo);
773         if (statinfo.st_mtim.tv_sec == PARAM_0) {
774             toCppResult = PARAM_1;
775         }
776     }
777     napi_value result = nullptr;
778     napi_create_int32(env, toCppResult, &result);
779     return result;
780 }
781 
782 extern "C" int __utimes_time64(const char *, const struct timeval[2]);
UTimes_time64(napi_env env,napi_callback_info info)783 static napi_value UTimes_time64(napi_env env, napi_callback_info info)
784 {
785     int fileDescribe = open(TEST_FILE, O_RDWR | O_RSYNC | O_CREAT);
786     close(fileDescribe);
787     struct timeval tv[2];
788     tv[0].tv_usec = PARAM_1;
789     int ret = __utimes_time64(TEST_FILE, tv);
790     napi_value result = nullptr;
791     napi_create_int32(env, ret, &result);
792     remove(TEST_FILE);
793     return result;
794 }
795 
796 extern "C" pid_t __wait4_time64(pid_t, int *, int, struct rusage *);
Wait4_time64(napi_env env,napi_callback_info info)797 static napi_value Wait4_time64(napi_env env, napi_callback_info info)
798 {
799     pid_t backParam, firstParam;
800     int secondParam = PARAM_1;
801     int thirdParam = PARAM_1;
802     struct rusage fourthParam {};
803     firstParam = getpid();
804     backParam = __wait4_time64(firstParam, &secondParam, thirdParam, &fourthParam);
805     napi_value result = nullptr;
806     napi_create_int32(env, backParam, &result);
807     return result;
808 }
809 
810 EXTERN_C_START
Init(napi_env env,napi_value exports)811 static napi_value Init(napi_env env, napi_value exports)
812 {
813     napi_property_descriptor desc[] = {
814         {"fnmatch", nullptr, FnMatch, nullptr, nullptr, nullptr, napi_default, nullptr},
815         {"__clock_gettime64", nullptr, clock_getTime64, nullptr, nullptr, nullptr, napi_default, nullptr},
816         {"__pthread_cond_timedwait_time64", nullptr, PThread_cond_timedWait_time64, nullptr, nullptr, nullptr,
817          napi_default, nullptr},
818         {"__nanosleep_time64", nullptr, NanoSleep_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
819         {"__fstat_time64", nullptr, FStat_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
820         {"__lstat_time64", nullptr, LStat_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
821         {"__stat_time64", nullptr, Stat_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
822         {"__utimensat_time64", nullptr, UTimeNsAt_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
823         {"__clock_getres_time64", nullptr, Clock_getRes_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
824         {"__clock_nanosleep_time64", nullptr, Clock_nanoSleep_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
825         {"__clock_settime64", nullptr, Clock_setTime64, nullptr, nullptr, nullptr, napi_default, nullptr},
826         {"__cnd_timedwait_time64", nullptr, Cnd_timedWait_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
827         {"__ctime64", nullptr, CTime64, nullptr, nullptr, nullptr, napi_default, nullptr},
828         {"__ctime64_r", nullptr, CTime64_r, nullptr, nullptr, nullptr, napi_default, nullptr},
829         {"__difftime64", nullptr, DiffTime64, nullptr, nullptr, nullptr, napi_default, nullptr},
830         {"__dlsym_time64", nullptr, DlSym_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
831         {"__futimens_time64", nullptr, FuTimeNs_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
832         {"__futimes_time64", nullptr, FuTimes_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
833         {"__futimesat_time64", nullptr, FuTimesAt_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
834         {"__gmtime64", nullptr, GmTime64, nullptr, nullptr, nullptr, napi_default, nullptr},
835         {"__gmtime64_r", nullptr, GmTime64_r, nullptr, nullptr, nullptr, napi_default, nullptr},
836         {"__localtime64", nullptr, Localtime64, nullptr, nullptr, nullptr, napi_default, nullptr},
837         {"__localtime64_r", nullptr, Localtime64_r, nullptr, nullptr, nullptr, napi_default, nullptr},
838         {"__lutimes_time64", nullptr, LuTimes_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
839         {"__mktime64", nullptr, MkTime64, nullptr, nullptr, nullptr, napi_default, nullptr},
840         {"__mtx_timedlock_time64", nullptr, Mtx_timedLock_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
841         {"__ppoll_time64", nullptr, PPoll_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
842         {"__pthread_mutex_timedlock_time64", nullptr, PThread_mutex_timedLock_time64, nullptr, nullptr, nullptr,
843          napi_default, nullptr},
844         {"__pthread_rwlock_timedrdlock_time64", nullptr, PThread_rwLock_timedrdLock_time64, nullptr, nullptr, nullptr,
845          napi_default, nullptr},
846         {"__pthread_rwlock_timedwrlock_time64", nullptr, PThread_rwLock_timedWrLock_time64, nullptr, nullptr, nullptr,
847          napi_default, nullptr},
848         {"__recvmmsg_time64", nullptr, ReCvmMsg_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
849         {"__sched_rr_get_interval_time64", nullptr, SchEd_rr_get_interval_time64, nullptr, nullptr, nullptr,
850          napi_default, nullptr},
851         {"__select_time64", nullptr, Select_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
852         {"__sem_timedwait_time64", nullptr, Sem_timedWait_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
853         {"__semtimedop_time64", nullptr, SemTimeDop_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
854         {"__settimeofday_time64", nullptr, SetTimeOfDay_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
855         {"__sigtimedwait_time64", nullptr, SigTimedWait_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
856         {"__thrd_sleep_time64", nullptr, THrd_sleep_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
857         {"__time64", nullptr, Time64, nullptr, nullptr, nullptr, napi_default, nullptr},
858         {"__timegm_time64", nullptr, TimeGm_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
859         {"__timerfd_gettime64", nullptr, TimerFd_getTime64, nullptr, nullptr, nullptr, napi_default, nullptr},
860         {"__timerfd_settime64", nullptr, TimerFd_setTime64, nullptr, nullptr, nullptr, napi_default, nullptr},
861         {"__timespec_get_time64", nullptr, TimerSpec_get_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
862         {"__utime64", nullptr, UTime64, nullptr, nullptr, nullptr, napi_default, nullptr},
863         {"__utimes_time64", nullptr, UTimes_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
864         {"__wait4_time64", nullptr, Wait4_time64, nullptr, nullptr, nullptr, napi_default, nullptr},
865     };
866     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
867     return exports;
868 }
869 EXTERN_C_END
870 
871 static napi_module demoModule = {
872     .nm_version = 1,
873     .nm_flags = 0,
874     .nm_filename = nullptr,
875     .nm_register_func = Init,
876     .nm_modname = "libfnmatchndk",
877     .nm_priv = ((void *)0),
878     .reserved = {0},
879 };
880 
RegisterModule(void)881 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
882