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 "pthread.h"
18 #include <cstdio>
19 #include <js_native_api_types.h>
20 #include <poll.h>
21 #include <sys/prctl.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24
25 #define TEST_AT_FDCWD (-100)
26 #define TEST_ERROR_AT_FDCWD 100
27 #define NO_ERR 0
28 #define SUCCESS 1
29 #define FAIL (-1)
30 #define PARAM_0 0
31 #define TEN 10
32 #define BUFSIZE 32
33 #define TEST_FIFO_MODE 0666
34
35 int g_prctlRet = FAIL;
Thread(void *)36 void *Thread(void *)
37 {
38 char name[BUFSIZE] = "wenhao";
39 g_prctlRet = prctl(PR_SET_NAME, (unsigned long)name);
40 pthread_exit(&g_prctlRet);
41 }
42
Prctl(napi_env env, napi_callback_info info)43 static napi_value Prctl(napi_env env, napi_callback_info info)
44 {
45 void *pThreadResult = nullptr;
46 pthread_t tid = PARAM_0;
47 pthread_create(&tid, nullptr, Thread, nullptr);
48 pthread_join(tid, &pThreadResult);
49 napi_value result = nullptr;
50 napi_create_int32(env, *static_cast<int *>(pThreadResult), &result);
51 return result;
52 }
53
54 EXTERN_C_START
Init(napi_env env, napi_value exports)55 static napi_value Init(napi_env env, napi_value exports)
56 {
57 napi_property_descriptor desc[] = {
58 {"prctl", nullptr, Prctl, nullptr, nullptr, nullptr, napi_default, nullptr},
59
60 };
61 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
62 return exports;
63 }
64 EXTERN_C_END
65
66 static napi_module demoModule = {
67 .nm_version = 1,
68 .nm_flags = 0,
69 .nm_filename = nullptr,
70 .nm_register_func = Init,
71 .nm_modname = "prctl",
72 .nm_priv = ((void *)0),
73 .reserved = {0},
74 };
75
RegisterModule(void)76 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
77