• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 <cerrno>
18 #include <fcntl.h>
19 #include <js_native_api_types.h>
20 #include <sys/swap.h>
21 #include <unistd.h>
22 #include <hilog/log.h>
23 
24 #define INIT (-1)
25 #define FAIL (-1)
26 #define SUCCESS 0
27 #define PARAM_1 1
28 #define MPARAM_1 (-1)
29 #define PARAM_0777 0777
30 
31 #undef LOG_DOMAIN
32 #undef LOG_TAG
33 #define LOG_DOMAIN 0xFEFE
34 #define LOG_TAG "MUSL_LIBCTEST"
35 
SwapOn(napi_env env,napi_callback_info info)36 static napi_value SwapOn(napi_env env, napi_callback_info info)
37 {
38     int flags = SWAP_FLAG_PREFER, backParam = PARAM_1;
39     napi_value result = nullptr;
40     const char *path = "/data/storage/el2/base/files/Fzl.txt";
41     errno = 0;
42     int fd = open(path, O_CREAT, PARAM_0777);
43     if (fd == FAIL) {
44         OH_LOG_INFO(LOG_APP, "SwapOn open failed: fd %{public}d errno : %{public}d", fd, errno);
45         napi_create_int32(env, FAIL, &result);
46         return result;
47     }
48     // Test syscall encapsulation interface
49     backParam = swapon(path, flags);
50     close(fd);
51     napi_create_int32(env, backParam, &result);
52     return result;
53 }
54 
SwapOff(napi_env env,napi_callback_info info)55 static napi_value SwapOff(napi_env env, napi_callback_info info)
56 {
57     int flags = SWAP_FLAG_PREFER, checkParam, backParam = PARAM_1;
58     napi_value result = nullptr;
59     const char *path = "/data/storage/el2/base/files/Fzl.txt";
60     errno = 0;
61     int fd = open(path, O_CREAT, PARAM_0777);
62     if (fd == FAIL) {
63         OH_LOG_INFO(LOG_APP, "SwapOff open failed: fd %{public}d errno : %{public}d", fd, errno);
64         napi_create_int32(env, FAIL, &result);
65         return result;
66     }
67     // Test syscall encapsulation interface
68     checkParam = swapon(path, flags);
69     if (checkParam != MPARAM_1) {
70         backParam = swapoff(path);
71     }
72     close(fd);
73     napi_create_int32(env, backParam, &result);
74     return result;
75 }
76 
77 EXTERN_C_START
Init(napi_env env,napi_value exports)78 static napi_value Init(napi_env env, napi_value exports)
79 {
80     napi_property_descriptor desc[] = {
81         {"swapon", nullptr, SwapOn, nullptr, nullptr, nullptr, napi_default, nullptr},
82         {"swapoff", nullptr, SwapOff, nullptr, nullptr, nullptr, napi_default, nullptr},
83     };
84     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
85     return exports;
86 }
87 EXTERN_C_END
88 
89 static napi_module demoModule = {
90     .nm_version = 1,
91     .nm_flags = 0,
92     .nm_filename = nullptr,
93     .nm_register_func = Init,
94     .nm_modname = "libswapndk",
95     .nm_priv = ((void *)0),
96     .reserved = {0},
97 };
98 
RegisterModule(void)99 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
100