• 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 "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 = {.rlim_cur = lim, .rlim_max = lim};
100     int ret = prlimit(getpid(), RLIMIT_STACK, &newLimit, nullptr);
101     napi_value result = nullptr;
102     napi_create_int32(env, ret, &result);
103     return result;
104 }
PrLimit64(napi_env env,napi_callback_info info)105 static napi_value PrLimit64(napi_env env, napi_callback_info info)
106 {
107     static const unsigned long long lim = PARAM_4;
108     struct rlimit newLimit = {.rlim_cur = lim, .rlim_max = lim};
109     int ret = prlimit64(getpid(), RLIMIT_STACK, &newLimit, nullptr);
110     napi_value result = nullptr;
111     napi_create_int32(env, ret, &result);
112     return result;
113 }
Getrlimit64(napi_env env,napi_callback_info info)114 static napi_value Getrlimit64(napi_env env, napi_callback_info info)
115 {
116     static const long lim = PARAM_42;
117     static const int r = RLIMIT_NOFILE;
118     struct rlimit rl = {PARAM_0};
119     rl.rlim_max = lim;
120     rl.rlim_cur = lim;
121     int ret = setrlimit(r, &rl);
122     struct rlimit retrl = {PARAM_0};
123     ret = getrlimit64(r, &retrl);
124     napi_value result = nullptr;
125     napi_create_int32(env, ret, &result);
126     return result;
127 }
Setpriority(napi_env env,napi_callback_info info)128 static napi_value Setpriority(napi_env env, napi_callback_info info)
129 {
130     size_t argc = PARAM_1;
131     napi_value args[1] = {nullptr};
132     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
133     int valueFirst = PARAM_0;
134     napi_get_value_int32(env, args[0], &valueFirst);
135     int oldprio = getpriority(PRIO_PROCESS, getpid());
136     int setval = setpriority(PRIO_PROCESS, getpid(), oldprio + valueFirst);
137     napi_value result = nullptr;
138     napi_create_int32(env, setval, &result);
139     return result;
140 }
141 
Setrlimit(napi_env env,napi_callback_info info)142 static napi_value Setrlimit(napi_env env, napi_callback_info info)
143 {
144     size_t argc = PARAM_1;
145     napi_value args[1] = {nullptr};
146     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
147     int valueFirst = PARAM_0;
148     napi_get_value_int32(env, args[0], &valueFirst);
149     napi_value result = nullptr;
150     if (valueFirst == PARAM_0) {
151         static const long lim = FOURTWO;
152         static const int r = RLIMIT_NOFILE;
153         struct rlimit rl = {PARAM_0};
154         rl.rlim_max = lim;
155         rl.rlim_cur = lim;
156         int setval = setrlimit(r, &rl);
157         napi_create_int32(env, setval, &result);
158     } else if (valueFirst == ONEVAL) {
159         int r = RLIMIT_NOFILE;
160         long lim = PARAM_0;
161         long lim1 = SIZE_1024;
162         struct rlimit rl = {PARAM_0};
163         rl.rlim_max = lim;
164         rl.rlim_cur = lim1;
165         int setval = setrlimit(r, &rl);
166         napi_create_int32(env, setval, &result);
167     }
168     return result;
169 }
170 
Setrlimit64(napi_env env,napi_callback_info info)171 static napi_value Setrlimit64(napi_env env, napi_callback_info info)
172 {
173     size_t argc = PARAM_1;
174     napi_value args[1] = {nullptr};
175     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
176     int valueFirst = PARAM_0;
177     napi_get_value_int32(env, args[0], &valueFirst);
178     napi_value result = nullptr;
179     if (valueFirst == PARAM_0) {
180         static const long lim = FOURTWO;
181         static const int r = RLIMIT_NOFILE;
182         struct rlimit rl = {PARAM_0};
183         rl.rlim_max = lim;
184         rl.rlim_cur = lim;
185         int setval = setrlimit64(r, &rl);
186         napi_create_int32(env, setval, &result);
187     } else if (valueFirst == ONEVAL) {
188         int r = RLIMIT_NOFILE;
189         long lim = PARAM_0;
190         long lim1 = SIZE_1024;
191         struct rlimit rl = {PARAM_0};
192         rl.rlim_max = lim;
193         rl.rlim_cur = lim1;
194         int setval = setrlimit64(r, &rl);
195         napi_create_int32(env, setval, &result);
196     }
197     return result;
198 }
199 
200 EXTERN_C_START
Init(napi_env env,napi_value exports)201 static napi_value Init(napi_env env, napi_value exports)
202 {
203     napi_property_descriptor desc[] = {
204         {"getPriority", nullptr, GetPriority, nullptr, nullptr, nullptr, napi_default, nullptr},
205         {"getRLimit", nullptr, GetRLimit, nullptr, nullptr, nullptr, napi_default, nullptr},
206         {"getRUsage", nullptr, GetRUsage, nullptr, nullptr, nullptr, napi_default, nullptr},
207         {"setpriority", nullptr, Setpriority, nullptr, nullptr, nullptr, napi_default, nullptr},
208         {"setrlimit", nullptr, Setrlimit, nullptr, nullptr, nullptr, napi_default, nullptr},
209         {"setrlimit64", nullptr, Setrlimit64, nullptr, nullptr, nullptr, napi_default, nullptr},
210         {"getRUsage", nullptr, GetRUsage, nullptr, nullptr, nullptr, napi_default, nullptr},
211         {"prLimit", nullptr, PrLimit, nullptr, nullptr, nullptr, napi_default, nullptr},
212         {"getRUsage", nullptr, GetRUsage, nullptr, nullptr, nullptr, napi_default, nullptr},
213         {"getPriority", nullptr, GetPriority, nullptr, nullptr, nullptr, napi_default, nullptr},
214         {"getRLimit", nullptr, GetRLimit, nullptr, nullptr, nullptr, napi_default, nullptr},
215         {"getRUsage", nullptr, GetRUsage, nullptr, nullptr, nullptr, napi_default, nullptr},
216         {"getrlimit64", nullptr, Getrlimit64, nullptr, nullptr, nullptr, napi_default, nullptr},
217         {"getRLimit", nullptr, GetRLimit, nullptr, nullptr, nullptr, napi_default, nullptr},
218         {"prLimit64", nullptr, PrLimit64, nullptr, nullptr, nullptr, napi_default, nullptr},
219 
220     };
221     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
222     return exports;
223 }
224 
225 EXTERN_C_END
226 
227 static napi_module demoModule = {
228     .nm_version = 1,
229     .nm_flags = 0,
230     .nm_filename = nullptr,
231     .nm_register_func = Init,
232     .nm_modname = "resource",
233     .nm_priv = ((void *)0),
234     .reserved = {0},
235 };
236 
RegisterModule(void)237 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
238