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 <js_native_api.h>
18 #include <node_api.h>
19 #include <sys/resource.h>
20 #include <sys/wait.h>
21 #include <unistd.h>
22
23 #define PARAM_0 0
24 #define PARAM_1 1
25 #define NO_ERR 0
26 #define SUCCESS 1
27 #define FAIL (-1)
do_plain_tests(int (* fn1)(void * arg),void * arg1,int (* fn2)(void * arg),void * arg2)28 int do_plain_tests(int (*fn1)(void *arg), void *arg1, int (*fn2)(void *arg), void *arg2)
29 {
30 int ret = PARAM_0;
31 int pid = PARAM_0;
32 pid = fork();
33 if (pid == FAIL) {
34 return FAIL;
35 }
36 if (pid == PARAM_0) {
37 _exit(fn1(arg1));
38 }
39 if (fn2) {
40 ret = fn2(arg2);
41 }
42 return ret;
43 }
44
waittest(void * testarg)45 int waittest(void *testarg)
46 {
47 int flag = PARAM_0;
48 pid_t wait_for_pind = wait(&flag);
49 return wait_for_pind;
50 }
51
Wait(napi_env env,napi_callback_info info)52 static napi_value Wait(napi_env env, napi_callback_info info)
53 {
54 void *test = nullptr;
55 do_plain_tests(waittest, test, nullptr, nullptr);
56 napi_value result = nullptr;
57 napi_create_int32(env, SUCCESS, &result);
58 return result;
59 }
wait4test(void * testarg)60 int wait4test(void *testarg)
61 {
62 pid_t pid = fork();
63 int status = PARAM_0;
64 int options = PARAM_0;
65 struct rusage ru;
66 pid_t wait4ForPind = wait4(pid, &status, options, &ru);
67 return wait4ForPind;
68 }
69
Wait4(napi_env env,napi_callback_info info)70 static napi_value Wait4(napi_env env, napi_callback_info info)
71 {
72 void *test = nullptr;
73 do_plain_tests(wait4test, test, nullptr, nullptr);
74 napi_value result = nullptr;
75 napi_create_int32(env, SUCCESS, &result);
76 return result;
77 }
78
Wait3test(void * testarg)79 int Wait3test(void *testarg)
80 {
81 int status = PARAM_0;
82 int options = PARAM_0;
83 struct rusage ru;
84 pid_t wait3_for_pind = wait3(&status, options, &ru);
85 return wait3_for_pind;
86 }
87
Wait3(napi_env env,napi_callback_info info)88 static napi_value Wait3(napi_env env, napi_callback_info info)
89 {
90 void *test = nullptr;
91 do_plain_tests(Wait3test, test, nullptr, nullptr);
92 napi_value result = nullptr;
93 napi_create_int32(env, SUCCESS, &result);
94 return result;
95 }
96
waitidtest(void * testarg)97 int waitidtest(void *testarg)
98 {
99 siginfo_t si = {};
100 pid_t pid = fork();
101 int result = waitid(P_PID, pid, &si, WEXITED);
102 return result;
103 }
104
Waitid(napi_env env,napi_callback_info info)105 static napi_value Waitid(napi_env env, napi_callback_info info)
106 {
107 void *test = nullptr;
108 do_plain_tests(waitidtest, test, nullptr, nullptr);
109 napi_value result = nullptr;
110 napi_create_int32(env, SUCCESS, &result);
111 return result;
112 }
113
waitpidtest(void * testarg)114 int waitpidtest(void *testarg)
115 {
116 int status = PARAM_0;
117 int options = PARAM_0;
118 pid_t pid = fork();
119 pid_t waitpid_for_pind = waitpid(pid, &status, options);
120 return waitpid_for_pind;
121 }
122
Waitpid(napi_env env,napi_callback_info info)123 static napi_value Waitpid(napi_env env, napi_callback_info info)
124 {
125 void *test = nullptr;
126 do_plain_tests(waitpidtest, test, nullptr, nullptr);
127
128 napi_value result = nullptr;
129 napi_create_int32(env, SUCCESS, &result);
130 return result;
131 }
132
133 EXTERN_C_START
Init(napi_env env,napi_value exports)134 static napi_value Init(napi_env env, napi_value exports)
135 {
136 napi_property_descriptor desc[] = {
137 {"wait", nullptr, Wait, nullptr, nullptr, nullptr, napi_default, nullptr},
138 {"wait4", nullptr, Wait4, nullptr, nullptr, nullptr, napi_default, nullptr},
139 {"waitid", nullptr, Waitid, nullptr, nullptr, nullptr, napi_default, nullptr},
140 {"waitpid", nullptr, Waitpid, nullptr, nullptr, nullptr, napi_default, nullptr},
141 {"wait3", nullptr, Wait3, nullptr, nullptr, nullptr, napi_default, nullptr},
142 };
143 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
144 return exports;
145 }
146 EXTERN_C_END
147
148 static napi_module demoModule = {
149 .nm_version = 1,
150 .nm_flags = 0,
151 .nm_filename = nullptr,
152 .nm_register_func = Init,
153 .nm_modname = "waitndk",
154 .nm_priv = ((void *)0),
155 .reserved = {0},
156 };
157
RegisterModule(void)158 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
159