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 <mntent.h>
22 #include <node_api.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25
26 #define PARAM_0 0
27 #define PARAM_0777 0777
28 #define MPARAM_1 (-1)
29
30 static const char *TEMP_FILE = "/data/storage/el2/base/files/fzl.txt";
31 static const char *g_tempFileContent = "/data/storage/el2/base/files/fzl.txt none swap sw 0 0\n";
32
GetMnTent(napi_env env, napi_callback_info info)33 static napi_value GetMnTent(napi_env env, napi_callback_info info)
34 {
35 struct stat statbuff;
36 int fileDescribe = MPARAM_1;
37 FILE *fp = nullptr;
38 int ret = MPARAM_1;
39 do {
40 fileDescribe = open(TEMP_FILE, 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 fileDescribe = MPARAM_1;
54 ret = stat(TEMP_FILE, &statbuff);
55 if (ret == MPARAM_1) {
56 break;
57 }
58 ret = MPARAM_1;
59 fileDescribe = open(TEMP_FILE, O_CREAT, PARAM_0777);
60 if (fileDescribe == MPARAM_1) {
61 break;
62 }
63 fp = fdopen(fileDescribe, "r");
64 if (fp == nullptr) {
65 break;
66 }
67 struct mntent *m = getmntent(fp);
68 if (m != nullptr) {
69 ret = PARAM_0;
70 }
71 } while (PARAM_0);
72 if (fp != nullptr) {
73 endmntent(fp);
74 }
75 if (fileDescribe != MPARAM_1) {
76 close(fileDescribe);
77 }
78 remove(TEMP_FILE);
79 napi_value result = nullptr;
80 napi_create_int32(env, ret, &result);
81 return result;
82 }
83
GetMnTentR(napi_env env, napi_callback_info info)84 static napi_value GetMnTentR(napi_env env, napi_callback_info info)
85 {
86 struct stat statbuff;
87 int fileDescribe = MPARAM_1;
88 FILE *fp = nullptr;
89 int ret = MPARAM_1;
90 do {
91 fileDescribe = open(TEMP_FILE, O_CREAT | O_WRONLY, PARAM_0777);
92 if (fileDescribe == MPARAM_1) {
93 break;
94 }
95 ret = write(fileDescribe, g_tempFileContent, strlen(g_tempFileContent));
96 if (ret == MPARAM_1) {
97 break;
98 }
99 ret = fsync(fileDescribe);
100 if (ret == MPARAM_1) {
101 break;
102 }
103 close(fileDescribe);
104 fileDescribe = MPARAM_1;
105 ret = stat(TEMP_FILE, &statbuff);
106 if (ret == MPARAM_1) {
107 break;
108 }
109 ret = MPARAM_1;
110 fileDescribe = open(TEMP_FILE, O_CREAT, PARAM_0777);
111 if (fileDescribe == MPARAM_1) {
112 break;
113 }
114 fp = fdopen(fileDescribe, "r");
115 if (fp == nullptr) {
116 break;
117 }
118 struct mntent mntbuf;
119 char buf[1024];
120 struct mntent *m = getmntent_r(fp, &mntbuf, buf, sizeof(buf));
121 if (m != nullptr) {
122 ret = PARAM_0;
123 }
124 } while (PARAM_0);
125 if (fp != nullptr) {
126 endmntent(fp);
127 }
128 if (fileDescribe != MPARAM_1) {
129 close(fileDescribe);
130 }
131 remove(TEMP_FILE);
132 napi_value result = nullptr;
133 napi_create_int32(env, ret, &result);
134 return result;
135 }
136
137 EXTERN_C_START
Init(napi_env env, napi_value exports)138 static napi_value Init(napi_env env, napi_value exports)
139 {
140 napi_property_descriptor desc[] = {
141 {"getMnTent", nullptr, GetMnTent, nullptr, nullptr, nullptr, napi_default, nullptr},
142 {"getMnTentR", nullptr, GetMnTentR, nullptr, nullptr, nullptr, napi_default, nullptr},
143 };
144 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
145 return exports;
146 }
147
148 EXTERN_C_END
149
150 static napi_module demoModule = {
151 .nm_version = 1,
152 .nm_flags = 0,
153 .nm_filename = nullptr,
154 .nm_register_func = Init,
155 .nm_modname = "mntent1",
156 .nm_priv = ((void *)0),
157 .reserved = {0},
158 };
159
RegisterEntryModule(void)160 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
161