• 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 <cstring>
20 #include <fcntl.h>
21 #include <js_native_api_types.h>
22 #include <poll.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 
26 #define TEST_AT_FDCWD (-100)
27 #define TEST_ERROR_AT_FDCWD 100
28 
29 #define NO_ERR 0
30 #define SUCCESS 1
31 #define FAIL (-1)
32 #define TEN 10
33 #define TEST_FIFO_MODE 0666
34 #define TVSEC 1
35 #define TVNSEC 0
36 #define PARAM_0 0
37 #define PARAM_1 1
38 #define PARAM_2 2
39 #define PARAM_UNNORMAL (-1)
40 #define ERRON_0 0
41 #define PARAM_100000000 100000000
42 #define PARAM_1000 1000
43 
Poll(napi_env env,napi_callback_info info)44 static napi_value Poll(napi_env env, napi_callback_info info)
45 {
46     int ret = PARAM_0;
47     errno = ERRON_0;
48     ret = poll(nullptr, PARAM_0, PARAM_1);
49     napi_value result = nullptr;
50     napi_create_int32(env, ret, &result);
51     return result;
52 }
Ppoll(napi_env env,napi_callback_info info)53 static napi_value Ppoll(napi_env env, napi_callback_info info)
54 {
55     int ret = PARAM_0;
56     int fd = open("ppoll_function_file", O_RDWR | O_CREAT, S_IRWXU);
57     struct pollfd pollfds[] = {{.fd = fd, .events = POLLIN, .revents = PARAM_0}};
58     struct timespec timeout = {PARAM_0};
59     timeout.tv_sec = TVSEC;
60     timeout.tv_nsec = TVNSEC;
61     ret = ppoll(pollfds, PARAM_1, &timeout, nullptr);
62     close(fd);
63     int res = access("ppoll_function_file", F_OK);
64     if (res != PARAM_UNNORMAL) {
65         remove("ppoll_function_file");
66     }
67     napi_value result = nullptr;
68     napi_create_int32(env, ret, &result);
69     return result;
70 }
PollChk(napi_env env,napi_callback_info info)71 static napi_value PollChk(napi_env env, napi_callback_info info)
72 {
73     int fd[2];
74     pipe(fd);
75 
76     int pollChk = PARAM_0;
77     int pid = fork();
78     if (pid == PARAM_UNNORMAL) {
79     } else if (pid == PARAM_0) {
80         close(fd[0]);
81         const char *message = "";
82         write(fd[1], message, strlen(message) + 1);
83         close(fd[1]);
84     } else {
85         close(fd[1]);
86         struct pollfd buf[2] = {{fd[0], POLLIN, PARAM_0}, {fd[0], POLLIN, PARAM_0}};
87         int ts = PARAM_1000;
88         pollChk = __poll_chk(buf, PARAM_1, ts, buf[0].fd);
89         char buff;
90         while (read(fd[0], &buff, PARAM_1) > PARAM_0)
91             ;
92         close(fd[0]);
93     }
94 
95     napi_value result = nullptr;
96     napi_create_int32(env, pollChk, &result);
97     return result;
98 }
99 
PpollChk(napi_env env,napi_callback_info info)100 static napi_value PpollChk(napi_env env, napi_callback_info info)
101 {
102     int fd[2];
103     pipe(fd);
104 
105     int pollChk = PARAM_0;
106     int pid = fork();
107     if (pid == PARAM_UNNORMAL) {
108     } else if (pid == PARAM_0) {
109         close(fd[0]);
110         const char *message = "";
111         write(fd[1], message, strlen(message) + 1);
112         close(fd[1]);
113     } else {
114         close(fd[1]);
115         struct pollfd buf[2] = {{fd[0], POLLIN, PARAM_0}, {fd[0], POLLIN, PARAM_0}};
116         struct timespec ts = {.tv_nsec = PARAM_100000000};
117         pollChk = __ppoll_chk(buf, PARAM_1, &ts, nullptr, buf[0].fd);
118         char buff;
119         while (read(fd[0], &buff, PARAM_1) > PARAM_0)
120             ;
121         close(fd[0]);
122     }
123 
124     napi_value result = nullptr;
125     napi_create_int32(env, pollChk, &result);
126     return result;
127 }
128 EXTERN_C_START
Init(napi_env env,napi_value exports)129 static napi_value Init(napi_env env, napi_value exports)
130 {
131     napi_property_descriptor desc[] = {
132         {"poll", nullptr, Poll, nullptr, nullptr, nullptr, napi_default, nullptr},
133         {"ppoll", nullptr, Ppoll, nullptr, nullptr, nullptr, napi_default, nullptr},
134         {"pollchk", nullptr, PollChk, nullptr, nullptr, nullptr, napi_default, nullptr},
135         {"ppollchk", nullptr, PpollChk, nullptr, nullptr, nullptr, napi_default, nullptr},
136     };
137     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
138     return exports;
139 }
140 EXTERN_C_END
141 
142 static napi_module demoModule = {
143     .nm_version = 1,
144     .nm_flags = 0,
145     .nm_filename = nullptr,
146     .nm_register_func = Init,
147     .nm_modname = "poll",
148     .nm_priv = ((void *)0),
149     .reserved = {0},
150 };
151 
RegisterModule(void)152 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
153