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 "common/napi_helper.cpp"
17 #include "common/native_common.h"
18 #include "napi/native_api.h"
19 #include <cstdio>
20 #include <cstring>
21 #include <sys/select.h>
22 #include <sys/stat.h>
23 #include <sys/uio.h>
24 #include <unistd.h>
25
26 #define ONEVAL 1
27 #define MINUSONE (-1)
28 #define PARAM_1 1
29 #define PARAM_5 5
30 #define TEST_AT_FDCWD (-100)
31 #define TEST_ERROR_AT_FDCWD 100
32 #define NO_ERR 0
33 #define SUCCESS 1
34 #define FAIL (-1)
35 #define PARAM_0 0
36 #define TEN 10
37 #define TEST_MODE 0666
38
Select(napi_env env, napi_callback_info info)39 static napi_value Select(napi_env env, napi_callback_info info)
40 {
41 size_t argc = PARAM_1;
42 napi_value args[1] = {nullptr};
43 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
44 fd_set rfds;
45 struct timeval tv;
46 FD_ZERO(&rfds);
47 FD_SET(PARAM_0, &rfds);
48 int first = PARAM_0;
49 napi_value result;
50 napi_get_value_int32(env, args[0], &first);
51 if (first == PARAM_0) {
52 tv.tv_sec = PARAM_0;
53 tv.tv_usec = PARAM_0;
54 napi_create_double(env, MINUSONE, &result);
55 } else {
56 tv.tv_sec = PARAM_5;
57 tv.tv_usec = PARAM_0;
58 int retval = select(ONEVAL, &rfds, nullptr, nullptr, &tv);
59 napi_create_int32(env, retval, &result);
60 }
61 return result;
62 }
PSelect(napi_env env, napi_callback_info info)63 static napi_value PSelect(napi_env env, napi_callback_info info)
64 {
65 fd_set set = {PARAM_0};
66 struct timespec timeout = {PARAM_0};
67 timeout.tv_sec = PARAM_5;
68 timeout.tv_nsec = PARAM_0;
69 FD_ZERO(&set);
70 FD_SET(STDOUT_FILENO, &set);
71 int ret = FAIL;
72 napi_value result = nullptr;
73 ret = pselect(STDOUT_FILENO + PARAM_1, nullptr, &set, nullptr, &timeout, nullptr);
74 if (ret != PARAM_1) {
75 napi_create_int32(env, FAIL, &result);
76 } else {
77 napi_create_int32(env, NO_ERR, &result);
78 }
79 return result;
80 }
81 EXTERN_C_START
Init(napi_env env, napi_value exports)82 static napi_value Init(napi_env env, napi_value exports)
83 {
84 napi_property_descriptor desc[] = {
85 {"select", nullptr, Select, nullptr, nullptr, nullptr, napi_default, nullptr},
86 {"pSelect", nullptr, PSelect, nullptr, nullptr, nullptr, napi_default, nullptr},
87
88 };
89 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
90 return exports;
91 }
92
93 EXTERN_C_END
94
95 static napi_module demoModule = {
96 .nm_version = 1,
97 .nm_flags = 0,
98 .nm_filename = nullptr,
99 .nm_register_func = Init,
100 .nm_modname = "select",
101 .nm_priv = ((void *)0),
102 .reserved = {0},
103 };
104
RegisterModule(void)105 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
106