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 <fcntl.h>
18 #include <js_native_api_types.h>
19 #include <sys/swap.h>
20
21 #define INIT (-1)
22 #define SUCCESS 0
23 #define PARAM_1 1
24 #define MPARAM_1 (-1)
25
SwapOn(napi_env env,napi_callback_info info)26 static napi_value SwapOn(napi_env env, napi_callback_info info)
27 {
28 int flags = SWAP_FLAG_PREFER, backParam = PARAM_1;
29 const char *path = "/data/storage/el2/base/files/Fzl.txt";
30 open(path, O_CREAT);
31 backParam = swapon(path, flags);
32 napi_value result = nullptr;
33 napi_create_int32(env, backParam, &result);
34 return result;
35 }
36
SwapOff(napi_env env,napi_callback_info info)37 static napi_value SwapOff(napi_env env, napi_callback_info info)
38 {
39 int flags = SWAP_FLAG_PREFER, checkParam, backParam = PARAM_1;
40 const char *path = "/data/storage/el2/base/files/Fzl.txt";
41 open(path, O_CREAT);
42 checkParam = swapon(path, flags);
43 if (checkParam != MPARAM_1) {
44 backParam = swapoff(path);
45 }
46 napi_value result = nullptr;
47 napi_create_int32(env, backParam, &result);
48 return result;
49 }
50
51 EXTERN_C_START
Init(napi_env env,napi_value exports)52 static napi_value Init(napi_env env, napi_value exports)
53 {
54 napi_property_descriptor desc[] = {
55 {"swapon", nullptr, SwapOn, nullptr, nullptr, nullptr, napi_default, nullptr},
56 {"swapoff", nullptr, SwapOff, nullptr, nullptr, nullptr, napi_default, nullptr},
57 };
58 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
59 return exports;
60 }
61 EXTERN_C_END
62
63 static napi_module demoModule = {
64 .nm_version = 1,
65 .nm_flags = 0,
66 .nm_filename = nullptr,
67 .nm_register_func = Init,
68 .nm_modname = "libswapndk",
69 .nm_priv = ((void *)0),
70 .reserved = {0},
71 };
72
RegisterModule(void)73 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
74