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(PARAM_0);
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     if (pid == PARAM_0) {
68         _exit(PARAM_0);
69     }
70     return wait4ForPind;
71 }
72 
Wait4(napi_env env, napi_callback_info info)73 static napi_value Wait4(napi_env env, napi_callback_info info)
74 {
75     void *test = nullptr;
76     do_plain_tests(wait4test, test, nullptr, nullptr);
77     napi_value result = nullptr;
78     napi_create_int32(env, SUCCESS, &result);
79     return result;
80 }
81 
Wait3test(void *testarg)82 int Wait3test(void *testarg)
83 {
84     int status = PARAM_0;
85     int options = PARAM_0;
86     struct rusage ru;
87     pid_t wait3_for_pind = wait3(&status, options, &ru);
88     return wait3_for_pind;
89 }
90 
Wait3(napi_env env, napi_callback_info info)91 static napi_value Wait3(napi_env env, napi_callback_info info)
92 {
93     void *test = nullptr;
94     do_plain_tests(Wait3test, test, nullptr, nullptr);
95     napi_value result = nullptr;
96     napi_create_int32(env, SUCCESS, &result);
97     return result;
98 }
99 
waitidtest(void *testarg)100 int waitidtest(void *testarg)
101 {
102     siginfo_t si = {};
103     pid_t pid = fork();
104     int result = waitid(P_PID, pid, &si, WEXITED);
105     if (pid == PARAM_0) {
106         _exit(PARAM_0);
107     }
108     return result;
109 }
110 
Waitid(napi_env env, napi_callback_info info)111 static napi_value Waitid(napi_env env, napi_callback_info info)
112 {
113     void *test = nullptr;
114     do_plain_tests(waitidtest, test, nullptr, nullptr);
115     napi_value result = nullptr;
116     napi_create_int32(env, SUCCESS, &result);
117     return result;
118 }
119 
waitpidtest(void *testarg)120 int waitpidtest(void *testarg)
121 {
122     int status = PARAM_0;
123     int options = PARAM_0;
124     pid_t pid = fork();
125     pid_t waitpid_for_pind = waitpid(pid, &status, options);
126     if (pid == PARAM_0) {
127         _exit(PARAM_0);
128     }
129     return waitpid_for_pind;
130 }
131 
Waitpid(napi_env env, napi_callback_info info)132 static napi_value Waitpid(napi_env env, napi_callback_info info)
133 {
134     void *test = nullptr;
135     do_plain_tests(waitpidtest, test, nullptr, nullptr);
136 
137     napi_value result = nullptr;
138     napi_create_int32(env, SUCCESS, &result);
139     return result;
140 }
141 
142 EXTERN_C_START
Init(napi_env env, napi_value exports)143 static napi_value Init(napi_env env, napi_value exports)
144 {
145     napi_property_descriptor desc[] = {
146         {"wait", nullptr, Wait, nullptr, nullptr, nullptr, napi_default, nullptr},
147         {"wait4", nullptr, Wait4, nullptr, nullptr, nullptr, napi_default, nullptr},
148         {"waitid", nullptr, Waitid, nullptr, nullptr, nullptr, napi_default, nullptr},
149         {"waitpid", nullptr, Waitpid, nullptr, nullptr, nullptr, napi_default, nullptr},
150         {"wait3", nullptr, Wait3, nullptr, nullptr, nullptr, napi_default, nullptr},
151     };
152     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
153     return exports;
154 }
155 EXTERN_C_END
156 
157 static napi_module demoModule = {
158     .nm_version = 1,
159     .nm_flags = 0,
160     .nm_filename = nullptr,
161     .nm_register_func = Init,
162     .nm_modname = "waitndk",
163     .nm_priv = ((void *)0),
164     .reserved = {0},
165 };
166 
RegisterModule(void)167 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
168