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 "common/napi_helper.cpp"
17 #include "common/native_common.h"
18 #include "napi/native_api.h"
19 #include <cstdio>
20 #include <ifaddrs.h>
21 #include <js_native_api_types.h>
22 #include <mntent.h>
23 #include <net/if.h>
24 #include <sys/inotify.h>
25 #include <unistd.h>
26 #include <utmp.h>
27 #include <uv.h>
28
29 #define ZEROVAL 0
30 #define ONEVAL 1
31 #define MINUSONE (-1)
32 #define PARAM_0 0
33 #define PARAM_1 1
34 #define FAIL (-1)
35 #define SIZE_1024 1024
36
Setmntent(napi_env env, napi_callback_info info)37 static napi_value Setmntent(napi_env env, napi_callback_info info)
38 {
39 FILE *ffp = setmntent("/data/storage/el2/base/files", "r");
40 napi_value result;
41 if (ffp != nullptr) {
42 napi_create_int32(env, ZEROVAL, &result);
43 } else {
44 napi_create_int32(env, ONEVAL, &result);
45 }
46 return result;
47 }
48
DoPlainTests(int (*fn1)(void *arg), void *arg1, int (*fn2)(void *arg), void *arg2)49 int DoPlainTests(int (*fn1)(void *arg), void *arg1, int (*fn2)(void *arg), void *arg2)
50 {
51 int ret = PARAM_0;
52 int pid = PARAM_0;
53 pid = fork();
54 if (pid == FAIL) {
55 return FAIL;
56 }
57 if (pid == PARAM_0) {
58 _exit(PARAM_0);
59 }
60 if (fn2) {
61 ret = fn2(arg2);
62 }
63 return ret;
64 }
65
getmntenttest(void *testarg)66 int getmntenttest(void *testarg)
67 {
68 FILE *mtab = nullptr;
69 struct mntent *part;
70 mtab = setmntent("/data/storage/el2/base/files/test.txt", "r");
71 part = getmntent(mtab);
72 endmntent(mtab);
73 if (part == nullptr) {
74 return FAIL;
75 } else {
76 return PARAM_0;
77 }
78 }
79
GetMnTent(napi_env env, napi_callback_info info)80 static napi_value GetMnTent(napi_env env, napi_callback_info info)
81 {
82 void *test = nullptr;
83 DoPlainTests(getmntenttest, test, nullptr, nullptr);
84 napi_value result = nullptr;
85 napi_create_int32(env, ZEROVAL, &result);
86 return result;
87 }
88
GetmntentRtest(void *testarg)89 int GetmntentRtest(void *testarg)
90 {
91 FILE *mtab = nullptr;
92 struct mntent *part = nullptr;
93 struct mntent mnt;
94 char strings[SIZE_1024];
95 mtab = setmntent("/data/storage/el2/base/files/test.txt", "r");
96 part = getmntent_r(mtab, &mnt, strings, sizeof(strings));
97 endmntent(mtab);
98 if (part == nullptr) {
99 return FAIL;
100 } else {
101 return PARAM_0;
102 }
103 }
104
GetMnTentR(napi_env env, napi_callback_info info)105 static napi_value GetMnTentR(napi_env env, napi_callback_info info)
106 {
107 void *test = nullptr;
108 DoPlainTests(GetmntentRtest, test, nullptr, nullptr);
109 napi_value result = nullptr;
110 napi_create_int32(env, ZEROVAL, &result);
111 return result;
112 }
113
Hasmntopt(napi_env env, napi_callback_info info)114 static napi_value Hasmntopt(napi_env env, napi_callback_info info)
115 {
116 char mnt_opts[] = "/data/storage/el2/base/files/test_hasmntopt";
117 struct mntent ent;
118 memset(&ent, PARAM_0, sizeof(ent));
119 ent.mnt_opts = mnt_opts;
120 char *ret = hasmntopt(&ent, "hasmntopt");
121 napi_value result;
122 if (ret == nullptr) {
123 napi_create_int32(env, FAIL, &result);
124 } else {
125 napi_create_int32(env, PARAM_0, &result);
126 }
127 return result;
128 }
129 EXTERN_C_START
Init(napi_env env, napi_value exports)130 static napi_value Init(napi_env env, napi_value exports)
131 {
132 napi_property_descriptor desc[] = {
133 {"setmntent", nullptr, Setmntent, nullptr, nullptr, nullptr, napi_default, nullptr},
134 {"getMnTent", nullptr, GetMnTent, nullptr, nullptr, nullptr, napi_default, nullptr},
135 {"getMnTentR", nullptr, GetMnTentR, nullptr, nullptr, nullptr, napi_default, nullptr},
136 {"hasmntopt", nullptr, Hasmntopt, nullptr, nullptr, nullptr, napi_default, nullptr},
137 };
138 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
139 return exports;
140 }
141
142 EXTERN_C_END
143
144 static napi_module demoModule = {
145 .nm_version = 1,
146 .nm_flags = 0,
147 .nm_filename = nullptr,
148 .nm_register_func = Init,
149 .nm_modname = "mntent",
150 .nm_priv = ((void *)0),
151 .reserved = {0},
152 };
153
RegisterEntryModule(void)154 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
155