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 <cerrno>
17 #include <js_native_api.h>
18 #include <node_api.h>
19 #include <pthread.h>
20 #include <sys/ptrace.h>
21 #include <unistd.h>
22
23 #define TEST_FLAG_SIZE 4
24 #define TEST_FD_LIMIT 128
25 #define R_OK 4
26 #define PARAM_0 0
27 #define MPARAM_1 (-1)
28 #define ERRON_0 0
29 #define PARAM_UNNORMAL (-1)
30
Ptrace(napi_env env, napi_callback_info info)31 static napi_value Ptrace(napi_env env, napi_callback_info info)
32 {
33 errno = ERRON_0;
34 int32_t ret = PARAM_0;
35 pid_t pid = fork();
36 if (pid == PARAM_0) {
37 ret = ptrace(PTRACE_TRACEME);
38 _exit(PARAM_0);
39 }
40 napi_value result;
41 if (MPARAM_1 == ret) {
42 napi_create_int32(env, PARAM_UNNORMAL, &result);
43 } else {
44 napi_create_int32(env, PARAM_0, &result);
45 }
46 return result;
47 }
48
49 EXTERN_C_START
Init(napi_env env, napi_value exports)50 static napi_value Init(napi_env env, napi_value exports)
51 {
52 napi_property_descriptor desc[] = {
53 {"ptrace", nullptr, Ptrace, nullptr, nullptr, nullptr, napi_default, nullptr},
54 };
55 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
56 return exports;
57 }
58 EXTERN_C_END
59
60 static napi_module demoModule = {
61 .nm_version = 1,
62 .nm_flags = 0,
63 .nm_filename = nullptr,
64 .nm_register_func = Init,
65 .nm_modname = "dirent",
66 .nm_priv = ((void *)0),
67 .reserved = {0},
68 };
69
RegisterEntryModule(void)70 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
71