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 <cstdio>
17 #include <cstring>
18 #include <fcntl.h>
19 #include <js_native_api.h>
20 #include <js_native_api_types.h>
21 #include <node_api.h>
22 #include <sys/mman.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 
26 #define MPARAM_1 (-1)
27 #define PARAM_0 0
28 #define PARAM_0777 0777
29 
30 static const char *g_tempFile = "/data/storage/el2/base/files/fzl.txt";
31 static const char *g_tempFileContent = "This is a test";
32 
Mmap(napi_env env, napi_callback_info info)33 static napi_value Mmap(napi_env env, napi_callback_info info)
34 {
35     struct stat statbuff;
36     int fileDescribe = MPARAM_1;
37     void *start = MAP_FAILED;
38     int ret = MPARAM_1;
39     do {
40         fileDescribe = open(g_tempFile, O_CREAT | O_WRONLY, PARAM_0777);
41         if (fileDescribe == MPARAM_1) {
42             break;
43         }
44         ret = write(fileDescribe, g_tempFileContent, strlen(g_tempFileContent));
45         if (ret == MPARAM_1) {
46             break;
47         }
48         ret = fsync(fileDescribe);
49         if (ret == MPARAM_1) {
50             break;
51         }
52         close(fileDescribe);
53         ret = stat(g_tempFile, &statbuff);
54         if (ret == MPARAM_1) {
55             break;
56         }
57         ret = MPARAM_1;
58         fileDescribe = open(g_tempFile, O_CREAT | O_RDWR, PARAM_0777);
59         if (fileDescribe == MPARAM_1) {
60             break;
61         }
62         start = mmap(nullptr, statbuff.st_size, PROT_READ, MAP_PRIVATE, fileDescribe, PARAM_0);
63         if (start == MAP_FAILED) {
64             break;
65         }
66         ret = PARAM_0;
67     } while (PARAM_0);
68     if (start != MAP_FAILED) {
69         munmap(start, statbuff.st_size);
70     }
71     if (fileDescribe != MPARAM_1) {
72         close(fileDescribe);
73     }
74     remove(g_tempFile);
75     napi_value result = nullptr;
76     napi_create_int32(env, ret, &result);
77     return result;
78 }
79 
80 EXTERN_C_START
Init(napi_env env, napi_value exports)81 static napi_value Init(napi_env env, napi_value exports)
82 {
83     napi_property_descriptor desc[] = {
84         {"mmap", nullptr, Mmap, nullptr, nullptr, nullptr, napi_default, nullptr},
85     };
86     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
87     return exports;
88 }
89 
90 EXTERN_C_END
91 
92 static napi_module demoModule = {
93     .nm_version = 1,
94     .nm_flags = 0,
95     .nm_filename = nullptr,
96     .nm_register_func = Init,
97     .nm_modname = "mman1",
98     .nm_priv = ((void *)0),
99     .reserved = {0},
100 };
101 
RegisterEntryModule(void)102 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
103