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 <cerrno>
20 #include <cstdlib>
21 #include <cstring>
22 #include <ifaddrs.h>
23 #include <js_native_api_types.h>
24 #include <net/if.h>
25 #include <sys/inotify.h>
26 #include <sys/resource.h>
27 #include <unistd.h>
28 #include <utmp.h>
29 #include <uv.h>
30
31 #define NO_ERR 0
32 #define SUCCESS 1
33 #define FAIL (-1)
34 #define TRUE 1
35 #define LENGTH 1
36 #define ONE 1
37 #define PARAM_0 0
38 #define PARAM_1 1
39 #define PARAM_4 4
40 #define PARAM_42 42
41 #define ONEVAL 1
42 #define MINUSONE (-1)
43 #define MINUSTWO (-2)
44 #define MINUSTHR (-3)
45 #define FOURTWO 42
46 #define SIZE_1024 1024
47
GetPriority(napi_env env, napi_callback_info info)48 static napi_value GetPriority(napi_env env, napi_callback_info info)
49 {
50 errno = NO_ERR;
51 size_t argc = PARAM_1;
52 napi_value args[1] = {nullptr};
53 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
54 int valueZero = PARAM_0;
55 napi_get_value_int32(env, args[0], &valueZero);
56 int ret = getpriority(valueZero, NO_ERR);
57 if ((ret = FAIL) && (errno)) {
58 ret = FAIL;
59 } else {
60 ret = TRUE;
61 }
62 napi_value result = nullptr;
63 napi_create_int32(env, ret, &result);
64 return result;
65 }
GetRLimit(napi_env env, napi_callback_info info)66 static napi_value GetRLimit(napi_env env, napi_callback_info info)
67 {
68 size_t argc = PARAM_1;
69 napi_value args[1] = {nullptr};
70 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
71
72 int valueZero = PARAM_0;
73 napi_get_value_int32(env, args[0], &valueZero);
74 size_t length = ONE;
75 struct rlimit rLim[length];
76 int ret = getrlimit(valueZero, rLim);
77 napi_value result = nullptr;
78 napi_create_int32(env, ret, &result);
79 return result;
80 }
GetRUsage(napi_env env, napi_callback_info info)81 static napi_value GetRUsage(napi_env env, napi_callback_info info)
82 {
83 size_t argc = PARAM_1;
84 napi_value args[1] = {nullptr};
85 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
86
87 int valueZero = PARAM_0;
88 napi_get_value_int32(env, args[0], &valueZero);
89 size_t length = LENGTH;
90 struct rusage rup[length];
91 int ret = getrusage(valueZero, rup);
92 napi_value result = nullptr;
93 napi_create_int32(env, ret, &result);
94 return result;
95 }
PrLimit(napi_env env, napi_callback_info info)96 static napi_value PrLimit(napi_env env, napi_callback_info info)
97 {
98 static const unsigned long long lim = PARAM_4;
99 struct rlimit newLimit = {0};
100 struct rlimit oldLimit = {0};
101 id_t pid = getpid();
102 int ret = prlimit(pid, RLIMIT_STACK, nullptr, &newLimit);
103 newLimit.rlim_cur = lim;
104 ret += prlimit(pid, RLIMIT_STACK, &newLimit, &oldLimit);
105 ret += prlimit(pid, RLIMIT_STACK, &oldLimit, nullptr);
106
107 napi_value result = nullptr;
108 napi_create_int32(env, ret, &result);
109 return result;
110 }
PrLimit64(napi_env env, napi_callback_info info)111 static napi_value PrLimit64(napi_env env, napi_callback_info info)
112 {
113 static const unsigned long long lim = PARAM_4;
114
115 struct rlimit newLimit = {0};
116 struct rlimit oldLimit = {0};
117 id_t pid = getpid();
118 int ret = prlimit64(pid, RLIMIT_STACK, nullptr, &newLimit);
119 newLimit.rlim_cur = lim;
120 ret += prlimit64(pid, RLIMIT_STACK, &newLimit, &oldLimit);
121 ret += prlimit64(pid, RLIMIT_STACK, &oldLimit, nullptr);
122
123 napi_value result = nullptr;
124 napi_create_int32(env, ret, &result);
125 return result;
126 }
Getrlimit64(napi_env env, napi_callback_info info)127 static napi_value Getrlimit64(napi_env env, napi_callback_info info)
128 {
129 static const long lim = PARAM_42;
130 static const int r = RLIMIT_NOFILE;
131 struct rlimit rl = {PARAM_0};
132 rl.rlim_max = lim;
133 rl.rlim_cur = lim;
134 int ret = setrlimit(r, &rl);
135 struct rlimit retrl = {PARAM_0};
136 ret = getrlimit64(r, &retrl);
137 napi_value result = nullptr;
138 napi_create_int32(env, ret, &result);
139 return result;
140 }
Setpriority(napi_env env, napi_callback_info info)141 static napi_value Setpriority(napi_env env, napi_callback_info info)
142 {
143 size_t argc = PARAM_1;
144 napi_value args[1] = {nullptr};
145 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
146 int valueFirst = PARAM_0;
147 napi_get_value_int32(env, args[0], &valueFirst);
148 int oldprio = getpriority(PRIO_PROCESS, getpid());
149 int setval = setpriority(PRIO_PROCESS, getpid(), oldprio + valueFirst);
150 napi_value result = nullptr;
151 napi_create_int32(env, setval, &result);
152 return result;
153 }
154
Setrlimit(napi_env env, napi_callback_info info)155 static napi_value Setrlimit(napi_env env, napi_callback_info info)
156 {
157 size_t argc = PARAM_1;
158 napi_value args[1] = {nullptr};
159 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
160 int valueFirst = PARAM_0;
161 napi_get_value_int32(env, args[0], &valueFirst);
162 napi_value result = nullptr;
163 if (valueFirst == PARAM_0) {
164 static const long lim = FOURTWO;
165 static const int r = RLIMIT_NOFILE;
166 struct rlimit rl = {PARAM_0};
167 rl.rlim_max = lim;
168 rl.rlim_cur = lim;
169 int setval = setrlimit(r, &rl);
170 napi_create_int32(env, setval, &result);
171 } else if (valueFirst == ONEVAL) {
172 int r = RLIMIT_NOFILE;
173 long lim = PARAM_0;
174 long lim1 = SIZE_1024;
175 struct rlimit rl = {PARAM_0};
176 rl.rlim_max = lim;
177 rl.rlim_cur = lim1;
178 int setval = setrlimit(r, &rl);
179 napi_create_int32(env, setval, &result);
180 }
181 return result;
182 }
183
Setrlimit64(napi_env env, napi_callback_info info)184 static napi_value Setrlimit64(napi_env env, napi_callback_info info)
185 {
186 size_t argc = PARAM_1;
187 napi_value args[1] = {nullptr};
188 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
189 int valueFirst = PARAM_0;
190 napi_get_value_int32(env, args[0], &valueFirst);
191 napi_value result = nullptr;
192 if (valueFirst == PARAM_0) {
193 static const long lim = FOURTWO;
194 static const int r = RLIMIT_NOFILE;
195 struct rlimit rl = {PARAM_0};
196 rl.rlim_max = lim;
197 rl.rlim_cur = lim;
198 int setval = setrlimit64(r, &rl);
199 napi_create_int32(env, setval, &result);
200 } else if (valueFirst == ONEVAL) {
201 int r = RLIMIT_NOFILE;
202 long lim = PARAM_0;
203 long lim1 = SIZE_1024;
204 struct rlimit rl = {PARAM_0};
205 rl.rlim_max = lim;
206 rl.rlim_cur = lim1;
207 int setval = setrlimit64(r, &rl);
208 napi_create_int32(env, setval, &result);
209 }
210 return result;
211 }
212
213 EXTERN_C_START
Init(napi_env env, napi_value exports)214 static napi_value Init(napi_env env, napi_value exports)
215 {
216 napi_property_descriptor desc[] = {
217 {"getPriority", nullptr, GetPriority, nullptr, nullptr, nullptr, napi_default, nullptr},
218 {"getRLimit", nullptr, GetRLimit, nullptr, nullptr, nullptr, napi_default, nullptr},
219 {"getRUsage", nullptr, GetRUsage, nullptr, nullptr, nullptr, napi_default, nullptr},
220 {"setpriority", nullptr, Setpriority, nullptr, nullptr, nullptr, napi_default, nullptr},
221 {"setrlimit", nullptr, Setrlimit, nullptr, nullptr, nullptr, napi_default, nullptr},
222 {"setrlimit64", nullptr, Setrlimit64, nullptr, nullptr, nullptr, napi_default, nullptr},
223 {"getRUsage", nullptr, GetRUsage, nullptr, nullptr, nullptr, napi_default, nullptr},
224 {"prLimit", nullptr, PrLimit, nullptr, nullptr, nullptr, napi_default, nullptr},
225 {"getRUsage", nullptr, GetRUsage, nullptr, nullptr, nullptr, napi_default, nullptr},
226 {"getPriority", nullptr, GetPriority, nullptr, nullptr, nullptr, napi_default, nullptr},
227 {"getRLimit", nullptr, GetRLimit, nullptr, nullptr, nullptr, napi_default, nullptr},
228 {"getRUsage", nullptr, GetRUsage, nullptr, nullptr, nullptr, napi_default, nullptr},
229 {"getrlimit64", nullptr, Getrlimit64, nullptr, nullptr, nullptr, napi_default, nullptr},
230 {"getRLimit", nullptr, GetRLimit, nullptr, nullptr, nullptr, napi_default, nullptr},
231 {"prLimit64", nullptr, PrLimit64, nullptr, nullptr, nullptr, napi_default, nullptr},
232
233 };
234 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
235 return exports;
236 }
237
238 EXTERN_C_END
239
240 static napi_module demoModule = {
241 .nm_version = 1,
242 .nm_flags = 0,
243 .nm_filename = nullptr,
244 .nm_register_func = Init,
245 .nm_modname = "resource",
246 .nm_priv = ((void *)0),
247 .reserved = {0},
248 };
249
RegisterModule(void)250 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
251