• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstdio>
18 #include <cstring>
19 #include <fcntl.h>
20 #include <js_native_api_types.h>
21 #include <sys/mount.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24 
25 #define NO_ERR 0
26 #define SUCCESS 1
27 #define FAIL (-1)
28 #define PARAM_0 0
29 #define PARAM_UNNORMAL (-1)
30 #define TEST_0777 0777
31 #define TEN 10
32 #define STATERROR (-100)
33 #define OPENERROR (-99)
34 #define MMAPERROR (-98)
35 #define TEST_SIZE 4096
36 
DoPlainTests(int (* fn1)(void * arg),void * arg1,int (* fn2)(void * arg),void * arg2)37 int DoPlainTests(int (*fn1)(void *arg), void *arg1, int (*fn2)(void *arg), void *arg2)
38 {
39     int ret = PARAM_0;
40     int pid = PARAM_0;
41     pid = fork();
42     if (pid == FAIL) {
43         return FAIL;
44     }
45     if (pid == PARAM_0) {
46         _exit(PARAM_0);
47     }
48     if (fn2) {
49         ret = fn2(arg2);
50     }
51     return ret;
52 }
53 
createTwoDir()54 void createTwoDir()
55 {
56     char path[] = "/data/storage/el2/base/files/mount1";
57     if (access(path, PARAM_0) != PARAM_0) {
58         mkdir(path, TEST_0777);
59     } else {
60         remove(path);
61         mkdir(path, TEST_0777);
62     }
63     char path2[] = "/data/storage/el2/base/files/mount2";
64     if (access(path2, PARAM_0) != PARAM_0) {
65         mkdir(path2, TEST_0777);
66     } else {
67         remove(path2);
68         mkdir(path2, TEST_0777);
69     }
70 }
71 
Mounttest(void * testarg)72 int Mounttest(void *testarg)
73 {
74     int rev = FAIL;
75     int retVal = FAIL;
76     createTwoDir();
77     const char special[] = "/data/storage/el2/base/files/mount1";
78     const char target[] = "/data/storage/el2/base/files/mount2";
79     rev = mount(special, target, "", PARAM_0, "");
80     retVal = rev;
81     if (rev == PARAM_0) {
82         rev = umount(special);
83     }
84     return retVal;
85 }
86 
Mount(napi_env env,napi_callback_info info)87 static napi_value Mount(napi_env env, napi_callback_info info)
88 {
89     void *test1 = nullptr;
90     DoPlainTests(Mounttest, test1, nullptr, nullptr);
91     napi_value result = nullptr;
92     napi_create_int32(env, PARAM_0, &result);
93     return result;
94 }
95 
umounttest(void * testarg)96 int umounttest(void *testarg)
97 {
98     int rev = FAIL;
99     int retVal = FAIL;
100     createTwoDir();
101     const char special[] = "/data/storage/el2/base/files/mount1";
102     const char target[] = "/data/storage/el2/base/files/mount2";
103     rev = mount(special, target, "", PARAM_0, "");
104     if (rev == PARAM_0) {
105         retVal = umount(special);
106     }
107     return retVal;
108 }
109 
Umount(napi_env env,napi_callback_info info)110 static napi_value Umount(napi_env env, napi_callback_info info)
111 {
112     void *test1 = nullptr;
113     DoPlainTests(umounttest, test1, nullptr, nullptr);
114     napi_value result = nullptr;
115     napi_create_int32(env, PARAM_0, &result);
116     return result;
117 }
118 
umount2test(void * testarg)119 int umount2test(void *testarg)
120 {
121     int rev = FAIL;
122     createTwoDir();
123     const char special[] = "/data/storage/el2/base/files/mount1";
124     const char target[] = "/data/storage/el2/base/files/mount2";
125     rev = mount(special, target, "", PARAM_0, "");
126     if (rev == PARAM_0) {
127         umount2(special, MNT_DETACH);
128     }
129     return rev;
130 }
131 
Umount2(napi_env env,napi_callback_info info)132 static napi_value Umount2(napi_env env, napi_callback_info info)
133 {
134     void *test1 = nullptr;
135     DoPlainTests(umount2test, test1, nullptr, nullptr);
136     napi_value result = nullptr;
137     napi_create_int32(env, PARAM_0, &result);
138     return result;
139 }
140 EXTERN_C_START
Init(napi_env env,napi_value exports)141 static napi_value Init(napi_env env, napi_value exports)
142 {
143     napi_property_descriptor desc[] = {
144         {"mount", nullptr, Mount, nullptr, nullptr, nullptr, napi_default, nullptr},
145         {"umount", nullptr, Umount, nullptr, nullptr, nullptr, napi_default, nullptr},
146         {"umount2", nullptr, Umount2, nullptr, nullptr, nullptr, napi_default, nullptr},
147     };
148     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
149     return exports;
150 }
151 EXTERN_C_END
152 
153 static napi_module demoModule = {
154     .nm_version = 1,
155     .nm_flags = 0,
156     .nm_filename = nullptr,
157     .nm_register_func = Init,
158     .nm_modname = "mman",
159     .nm_priv = ((void *)0),
160     .reserved = {0},
161 };
162 
RegisterModule(void)163 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
164