• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 <cassert>
17 #include <vector>
18 #include <grp.h>
19 #include <sys/types.h>
20 #include <unistd.h>
21 #include "js_childprocess.h"
22 #include "js_process.h"
23 #include "securec.h"
24 #include "utils/log.h"
25 
26 namespace OHOS::Js_sys_module::Process {
DealType(napi_env env,napi_value args[],size_t argsSize)27     static napi_value DealType(napi_env env, napi_value args[], size_t argsSize)
28     {
29         if (args[0] != nullptr) {
30             napi_valuetype valueType = napi_undefined;
31             NAPI_CALL(env, napi_typeof(env, args[0], &valueType));
32             NAPI_ASSERT(env, valueType == napi_string, "Wrong argument typr. String expected");
33         } else {
34             HILOG_ERROR("command is null");
35             NAPI_CALL(env, napi_throw_error(env, "", "command is empty"));
36         }
37 
38         std::vector<std::string> keyStr = {"timeout", "killSignal", "maxBuffer"};
39 
40         if (args[1] == nullptr) {
41             return nullptr;
42         }
43         size_t size = keyStr.size();
44         for (size_t i = 0; i < size; i++) {
45             napi_valuetype propertyType = napi_undefined;
46             napi_value property = nullptr;
47             NAPI_CALL(env, napi_get_named_property(env, args[1], keyStr[i].c_str(), &property));
48             switch (i) {
49                 case 0:
50                     {
51                         NAPI_CALL(env, napi_typeof(env, property, &propertyType));
52                         NAPI_ASSERT(env, propertyType == napi_number || propertyType == napi_undefined,
53                                     "Wrong timeout argument typr. Number expected");
54                         int timeout = 0;
55                         NAPI_CALL(env, napi_get_value_int32(env, property, &timeout));
56                         if (timeout < 0) {
57                             NAPI_CALL(env, napi_throw_error(env, "", "options timeout is lessthen zero"));
58                         }
59                         break;
60                     }
61                 case 1:
62                     NAPI_CALL(env, napi_typeof(env, property, &propertyType));
63                     NAPI_ASSERT(env, propertyType == napi_string || propertyType == napi_number
64                                 || propertyType == napi_undefined,
65                                 "Wrong KillSignal argument typr. String or number expected");
66                     break;
67                 case 2: // 2:The parameter value
68                     NAPI_CALL(env, napi_typeof(env, property, &propertyType));
69                     NAPI_ASSERT(env, propertyType == napi_number || propertyType == napi_undefined,
70                                 "Wrong maxBuffer argument typr. Number expected");
71                     break;
72                 default:
73                     break;
74             }
75         }
76         return nullptr;
77     }
78 
ChildProcessConstructor(napi_env env,napi_callback_info info)79     static napi_value ChildProcessConstructor(napi_env env, napi_callback_info info)
80     {
81         napi_value thisVar = nullptr;
82         void* data = nullptr;
83         size_t argc = 2; // 2:The number of parameters is 2
84         napi_value args[2] = { nullptr }; // 2:The number of parameters is 2
85         NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, &data));
86 
87         DealType(env, args, argc);
88         auto objectInfo = new ChildProcess();
89 
90         objectInfo->InitOptionsInfo(env, args[1]);
91 
92         objectInfo->Spawn(env, args[0]);
93 
94         NAPI_CALL(env, napi_wrap(
95             env, thisVar, objectInfo,
96             [](napi_env env, void* data, void* hint) {
97                 auto objectResult = reinterpret_cast<ChildProcess*>(data);
98                 if (objectResult != nullptr) {
99                     delete objectResult;
100                     objectResult = nullptr;
101                 }
102             },
103             nullptr, nullptr));
104 
105         return thisVar;
106     }
107 
Wait(napi_env env,napi_callback_info info)108     static napi_value Wait(napi_env env, napi_callback_info info)
109     {
110         napi_value thisVar = nullptr;
111         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
112 
113         ChildProcess* object = nullptr;
114         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object)));
115         napi_value result = object->Wait(env);
116 
117         return result;
118     }
119 
GetOutput(napi_env env,napi_callback_info info)120     static napi_value GetOutput(napi_env env, napi_callback_info info)
121     {
122         napi_value thisVar = nullptr;
123         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
124 
125         ChildProcess* object = nullptr;
126         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object)));
127         napi_value result = object->GetOutput(env);
128 
129         return result;
130     }
131 
Close(napi_env env,napi_callback_info info)132     static napi_value Close(napi_env env, napi_callback_info info)
133     {
134         napi_value thisVar = nullptr;
135         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
136 
137         ChildProcess* object = nullptr;
138         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object)));
139         object->Close();
140 
141         napi_value result = nullptr;
142         NAPI_CALL(env, napi_get_undefined(env, &result));
143         return result;
144     }
145 
GetErrorOutput(napi_env env,napi_callback_info info)146     static napi_value GetErrorOutput(napi_env env, napi_callback_info info)
147     {
148         napi_value thisVar = nullptr;
149         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
150 
151         ChildProcess* object = nullptr;
152         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object)));
153 
154         napi_value result = object->GetErrorOutput(env);
155 
156         return result;
157     }
158 
Kill(napi_env env,napi_callback_info info)159     static napi_value Kill(napi_env env, napi_callback_info info)
160     {
161         napi_value thisVar = nullptr;
162         size_t requireArgc = 1;
163         size_t argc = 1;
164         napi_value args = nullptr;
165         NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr));
166 
167         NAPI_ASSERT(env, argc >= requireArgc, "Wrong number of arguments");
168 
169         napi_valuetype valuetype;
170         NAPI_CALL(env, napi_typeof(env, args, &valuetype));
171         if ((valuetype != napi_valuetype::napi_number) && (valuetype != napi_valuetype::napi_string)) {
172             napi_throw_error(env, nullptr, "The parameter type is incorrect");
173         }
174 
175         ChildProcess* object = nullptr;
176         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object)));
177         object->Kill(env, args);
178 
179         napi_value result = nullptr;
180         NAPI_CALL(env, napi_get_undefined(env, &result));
181         return result;
182     }
183 
GetKilled(napi_env env,napi_callback_info info)184     static napi_value GetKilled(napi_env env, napi_callback_info info)
185     {
186         napi_value thisVar = nullptr;
187         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
188 
189         ChildProcess* object = nullptr;
190         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object)));
191         napi_value result = object->GetKilled(env);
192 
193         return result;
194     }
195 
Getpid(napi_env env,napi_callback_info info)196     static napi_value Getpid(napi_env env, napi_callback_info info)
197     {
198         napi_value thisVar = nullptr;
199         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
200 
201         ChildProcess* object = nullptr;
202         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object)));
203         napi_value result = object->Getpid(env);
204 
205         return result;
206     }
207 
Getppid(napi_env env,napi_callback_info info)208     static napi_value Getppid(napi_env env, napi_callback_info info)
209     {
210         napi_value thisVar = nullptr;
211         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
212 
213         ChildProcess* object = nullptr;
214         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object)));
215         napi_value result = object->Getppid(env);
216 
217         return result;
218     }
219 
GetExitCode(napi_env env,napi_callback_info info)220     static napi_value GetExitCode(napi_env env, napi_callback_info info)
221     {
222         napi_value thisVar = nullptr;
223         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr));
224 
225         ChildProcess* object = nullptr;
226         NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object)));
227         napi_value result = object->GetExitCode(env);
228 
229         return result;
230     }
231 
RunCommand(napi_env env,napi_callback_info info)232     static napi_value RunCommand(napi_env env, napi_callback_info info)
233     {
234         napi_value thisVar = nullptr;
235         size_t argc = 2; // 2:The number of parameters is 2
236         napi_value args[2] = { nullptr }; // 2:The number of parameters is 2
237         NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
238 
239         const char* childProcessClassName = "ChildProcess";
240         napi_value childProcessClass = nullptr;
241         napi_property_descriptor childProcessDesc[] = {
242             DECLARE_NAPI_FUNCTION("close", Close),
243             DECLARE_NAPI_FUNCTION("kill", Kill),
244             DECLARE_NAPI_FUNCTION("getOutput", GetOutput),
245             DECLARE_NAPI_FUNCTION("getErrorOutput", GetErrorOutput),
246             DECLARE_NAPI_FUNCTION("wait", Wait),
247             DECLARE_NAPI_GETTER("killed", GetKilled),
248             DECLARE_NAPI_GETTER("pid", Getpid),
249             DECLARE_NAPI_GETTER("ppid", Getppid),
250             DECLARE_NAPI_GETTER("exitCode", GetExitCode),
251         };
252 
253         NAPI_CALL(env, napi_define_class(env, childProcessClassName, strlen(childProcessClassName),
254                                          ChildProcessConstructor, nullptr,
255                                          sizeof(childProcessDesc) / sizeof(childProcessDesc[0]), childProcessDesc,
256                                          &childProcessClass));
257 
258         napi_value result = nullptr;
259         NAPI_CALL(env, napi_new_instance(env, childProcessClass, argc, args, &result));
260 
261         return result;
262     }
263 
GetUid(napi_env env,napi_callback_info info)264     static napi_value GetUid(napi_env env, [[maybe_unused]] napi_callback_info info)
265     {
266         Process object;
267         return object.GetUid(env);
268     }
269 
GetGid(napi_env env,napi_callback_info info)270     static napi_value GetGid(napi_env env, [[maybe_unused]] napi_callback_info info)
271     {
272         Process object;
273         return object.GetGid(env);
274     }
275 
GetEUid(napi_env env,napi_callback_info info)276     static napi_value GetEUid(napi_env env, [[maybe_unused]] napi_callback_info info)
277     {
278         Process object;
279         return object.GetEUid(env);
280     }
281 
GetEGid(napi_env env,napi_callback_info info)282     static napi_value GetEGid(napi_env env, [[maybe_unused]] napi_callback_info info)
283     {
284         Process object;
285         return object.GetEGid(env);
286     }
287 
GetGroups(napi_env env,napi_callback_info info)288     static napi_value GetGroups(napi_env env, [[maybe_unused]] napi_callback_info info)
289     {
290         Process object;
291         return object.GetGroups(env);
292     }
293 
GetPid(napi_env env,napi_callback_info info)294     static napi_value GetPid(napi_env env, [[maybe_unused]] napi_callback_info info)
295     {
296         Process object;
297         return object.GetPid(env);
298     }
299 
GetPpid(napi_env env,napi_callback_info info)300     static napi_value GetPpid(napi_env env, [[maybe_unused]] napi_callback_info info)
301     {
302         Process object;
303         return object.GetPpid(env);
304     }
305 
Chdir(napi_env env,napi_callback_info info)306     static napi_value Chdir(napi_env env, napi_callback_info info)
307     {
308         napi_value thisVar = nullptr;
309         size_t requireArgc = 1;
310         size_t argc = 1;
311         napi_value args = nullptr;
312         NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr));
313         NAPI_ASSERT(env, argc >= requireArgc, "Wrong nuamber of arguments");
314         napi_valuetype valuetype;
315         NAPI_CALL(env, napi_typeof(env, args, &valuetype));
316         NAPI_ASSERT(env, valuetype == napi_string, "Wrong argument type. String expected");
317         Process object;
318         object.Chdir(env, args);
319         napi_value result = nullptr;
320         NAPI_CALL(env, napi_get_undefined(env, &result));
321         return result;
322     }
323 
Abort(napi_env env,napi_callback_info info)324     static napi_value Abort(napi_env env, [[maybe_unused]] napi_callback_info info)
325     {
326         Process object;
327         object.Abort();
328         napi_value res = nullptr;
329         NAPI_CALL(env, napi_get_undefined(env, &res));
330         return res;
331     }
332 
Cwd(napi_env env,napi_callback_info info)333     static napi_value Cwd(napi_env env, [[maybe_unused]] napi_callback_info info)
334     {
335         Process object;
336         return object.Cwd(env);
337     }
338 
Exit(napi_env env,napi_callback_info info)339     static napi_value Exit(napi_env env, napi_callback_info info)
340     {
341         napi_value thisVar = nullptr;
342         size_t argc = 1;
343         napi_value args = nullptr;
344         napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr);
345         napi_valuetype valuetype;
346         NAPI_CALL(env, napi_typeof(env, args, &valuetype));
347         NAPI_ASSERT(env, valuetype == napi_number, "Wrong argument type.number error");
348         Process object;
349         object.Exit(env, args);
350         napi_value res = nullptr;
351         NAPI_CALL(env, napi_get_undefined(env, &res));
352         return res;
353     }
On(napi_env env,napi_callback_info info)354     static napi_value On(napi_env env, napi_callback_info info)
355     {
356         napi_value thisVar = nullptr;
357         bool flag = true;
358         napi_value result = nullptr;
359         size_t requireArgc = 2; // 2:The number of parameters is 2
360         size_t argc = 2; // 2:The number of parameters is 2
361         napi_value args[2] = { nullptr };
362         NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr));
363         NAPI_ASSERT(env, argc >= requireArgc, "Wrong number of arguments");
364         napi_valuetype valuetype0;
365         NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
366         if (valuetype0 != napi_valuetype::napi_string) {
367             flag = false;
368             NAPI_CALL(env, napi_get_boolean(env, flag, &result));
369             return result;
370         }
371         napi_valuetype valuetype1;
372         NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1));
373         Process object;
374         object.On(env, args[0], args[1]);
375         NAPI_CALL(env, napi_get_boolean(env, flag, &result));
376         return result;
377     }
378 
Off(napi_env env,napi_callback_info info)379     static napi_value Off(napi_env env, napi_callback_info info)
380     {
381         napi_value thisVar = nullptr;
382         size_t argc = 1;
383         napi_value args = nullptr;
384         napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr);
385         Process object;
386         napi_value result = object.Off(env, args);
387         return result;
388     }
389 
Uptime(napi_env env,napi_callback_info info)390     static napi_value Uptime(napi_env env, [[maybe_unused]] napi_callback_info info)
391     {
392         Process object;
393         return object.Uptime(env);
394     }
395 
KillSig(napi_env env,napi_callback_info info)396     static napi_value KillSig(napi_env env, napi_callback_info info)
397     {
398         size_t argc = 2; // 2:The number of parameters is 2
399         napi_value argv[2] = {0}; // 2:The number of parameters is 2
400         napi_value thisVar = nullptr;
401         void* data = nullptr;
402         napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
403         Process object;
404         napi_value result = nullptr;
405         result = object.Kill(env, argv[0], argv[1]);
406         return result;
407     }
GetTid(napi_env env,napi_callback_info info)408     static napi_value GetTid(napi_env env, [[maybe_unused]] napi_callback_info info)
409     {
410         Process object;
411         return object.GetTid(env);
412     }
413 
IsIsolatedProcess(napi_env env,napi_callback_info info)414     static napi_value IsIsolatedProcess(napi_env env, [[maybe_unused]] napi_callback_info info)
415     {
416         Process object;
417         return object.IsIsolatedProcess(env);
418     }
419 
IsAppUid(napi_env env,napi_callback_info info)420     static napi_value IsAppUid(napi_env env, napi_callback_info info)
421     {
422         napi_value thisVar = nullptr;
423         size_t argc = 1;
424         napi_value args = nullptr;
425         napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr);
426         napi_valuetype valuetype;
427         NAPI_CALL(env, napi_typeof(env, args, &valuetype));
428         NAPI_ASSERT(env, valuetype == napi_number, "Wrong argument type.String error");
429         Process object;
430         return object.IsAppUid(env, args);
431     }
432 
Is64Bit(napi_env env,napi_callback_info info)433     static napi_value Is64Bit(napi_env env, [[maybe_unused]] napi_callback_info info)
434     {
435         Process object;
436         return object.Is64Bit(env);
437     }
438 
GetUidForName(napi_env env,napi_callback_info info)439     static napi_value GetUidForName(napi_env env, napi_callback_info info)
440     {
441         napi_value thisVar = nullptr;
442         size_t argc = 1;
443         napi_value args = nullptr;
444         napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr);
445         napi_valuetype valuetype;
446         NAPI_CALL(env, napi_typeof(env, args, &valuetype));
447         NAPI_ASSERT(env, valuetype == napi_string, "Wrong argument type.String error");
448         Process object;
449         return object.GetUidForName(env, args);
450     }
451 
GetThreadPriority(napi_env env,napi_callback_info info)452     static napi_value GetThreadPriority(napi_env env, napi_callback_info info)
453     {
454         napi_value thisVar = nullptr;
455         size_t argc = 1;
456         napi_value args = nullptr;
457         napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr);
458         napi_valuetype valuetype;
459         NAPI_CALL(env, napi_typeof(env, args, &valuetype));
460         NAPI_ASSERT(env, valuetype == napi_number, "Wrong argument type.String error");
461         Process object;
462         return object.GetThreadPriority(env, args);
463     }
464 
GetStartRealtime(napi_env env,napi_callback_info info)465     static napi_value GetStartRealtime(napi_env env, [[maybe_unused]] napi_callback_info info)
466     {
467         Process object;
468         return object.GetStartRealtime(env);
469     }
470 
GetPastCputime(napi_env env,napi_callback_info info)471     static napi_value GetPastCputime(napi_env env, [[maybe_unused]] napi_callback_info info)
472     {
473         Process object;
474         return object.GetPastCputime(env);
475     }
476 
GetSystemConfig(napi_env env,napi_callback_info info)477     static napi_value GetSystemConfig(napi_env env, napi_callback_info info)
478     {
479         napi_value thisVar = nullptr;
480         size_t argc = 1;
481         napi_value args = nullptr;
482         napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr);
483         napi_valuetype valuetype;
484         NAPI_CALL(env, napi_typeof(env, args, &valuetype));
485         NAPI_ASSERT(env, valuetype == napi_number, "Wrong argument type.String error");
486         Process object;
487         return object.GetSystemConfig(env, args);
488     }
489 
GetEnvironmentVar(napi_env env,napi_callback_info info)490     static napi_value GetEnvironmentVar(napi_env env, napi_callback_info info)
491     {
492         napi_value thisVar = nullptr;
493         size_t argc = 1;
494         napi_value args = nullptr;
495         napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr);
496         napi_valuetype valuetype;
497         NAPI_CALL(env, napi_typeof(env, args, &valuetype));
498         NAPI_ASSERT(env, valuetype == napi_string, "Wrong argument type.String error");
499         Process object;
500         return object.GetEnvironmentVar(env, args);
501     }
502 
ThrowError(napi_env env,const char * errMessage)503     static napi_value ThrowError(napi_env env, const char* errMessage)
504     {
505         napi_value processError = nullptr;
506         napi_value code = nullptr;
507         uint32_t errCode = 401; // 401:The code parameter of this error is 401
508         napi_create_uint32(env, errCode, &code);
509         napi_value name = nullptr;
510         std::string errName = "BuisnessError";
511         napi_value msg = nullptr;
512         napi_create_string_utf8(env, errMessage, NAPI_AUTO_LENGTH, &msg);
513         napi_create_string_utf8(env, errName.c_str(), NAPI_AUTO_LENGTH, &name);
514         napi_create_error(env, nullptr, msg, &processError);
515         napi_set_named_property(env, processError, "code", code);
516         napi_set_named_property(env, processError, "name", name);
517         napi_throw(env, processError);
518         napi_value res = nullptr;
519         NAPI_CALL(env, napi_get_undefined(env, &res));
520         return res;
521     }
522 
GetValueFromInfo(napi_env env,napi_callback_info info,napi_value & thisVar)523     static napi_value GetValueFromInfo(napi_env env, napi_callback_info info, napi_value &thisVar)
524     {
525         size_t argc = 1;
526         napi_value args = nullptr;
527         napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr);
528         return args;
529     }
530 
KillSigOfProcess(napi_env env,napi_callback_info info)531     static napi_value KillSigOfProcess(napi_env env, napi_callback_info info)
532     {
533         size_t argc = 2; // 2:The number of parameters is 2
534         napi_value argv[2] = {0}; // 2:The number of parameters is 2
535         napi_value thisVar = nullptr;
536         void* data = nullptr;
537         napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
538         napi_valuetype valuetype0;
539         NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype0));
540         napi_valuetype valuetype1;
541         NAPI_CALL(env, napi_typeof(env, argv[1], &valuetype1));
542         if (valuetype0 != napi_number || valuetype1 != napi_number) {
543             return ThrowError(env, "The type of v must be number.");
544         }
545         ProcessManager *object = nullptr;
546         NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object));
547         return object->Kill(env, argv[0], argv[1]);
548     }
ExitOfProcess(napi_env env,napi_callback_info info)549     static napi_value ExitOfProcess(napi_env env, napi_callback_info info)
550     {
551         napi_value thisVar = nullptr;
552         napi_value args = GetValueFromInfo(env, info, thisVar);
553         napi_valuetype valuetype;
554         NAPI_CALL(env, napi_typeof(env, args, &valuetype));
555         if (valuetype != napi_number) {
556             return ThrowError(env, "The type of v must be number.");
557         }
558         ProcessManager *object = nullptr;
559         NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object));
560         object->Exit(env, args);
561         napi_value res = nullptr;
562         NAPI_CALL(env, napi_get_undefined(env, &res));
563         return res;
564     }
GetSystemConfigOfProcess(napi_env env,napi_callback_info info)565     static napi_value GetSystemConfigOfProcess(napi_env env, napi_callback_info info)
566     {
567         napi_value thisVar = nullptr;
568         napi_value args = GetValueFromInfo(env, info, thisVar);
569         napi_valuetype valuetype;
570         NAPI_CALL(env, napi_typeof(env, args, &valuetype));
571         if (valuetype != napi_number) {
572             return ThrowError(env, "The type of v must be number.");
573         }
574         ProcessManager *object = nullptr;
575         NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object));
576         return object->GetSystemConfig(env, args);
577     }
578 
GetThreadPriorityOfProcess(napi_env env,napi_callback_info info)579     static napi_value GetThreadPriorityOfProcess(napi_env env, napi_callback_info info)
580     {
581         napi_value thisVar = nullptr;
582         napi_value args = GetValueFromInfo(env, info, thisVar);
583         napi_valuetype valuetype;
584         NAPI_CALL(env, napi_typeof(env, args, &valuetype));
585         if (valuetype != napi_number) {
586             return ThrowError(env, "The type of v must be number.");
587         }
588         ProcessManager *object = nullptr;
589         NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object));
590         return object->GetThreadPriority(env, args);
591     }
592 
GetUidForNameOfProcess(napi_env env,napi_callback_info info)593     static napi_value GetUidForNameOfProcess(napi_env env, napi_callback_info info)
594     {
595         napi_value thisVar = nullptr;
596         napi_value args = GetValueFromInfo(env, info, thisVar);
597         napi_valuetype valuetype;
598         NAPI_CALL(env, napi_typeof(env, args, &valuetype));
599         if (valuetype != napi_string) {
600             return ThrowError(env, "The type of v must be string.");
601         }
602         ProcessManager *object = nullptr;
603         NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object));
604         return object->GetUidForName(env, args);
605     }
606 
IsAppUidOfProcess(napi_env env,napi_callback_info info)607     static napi_value IsAppUidOfProcess(napi_env env, napi_callback_info info)
608     {
609         napi_value thisVar = nullptr;
610         napi_value args = GetValueFromInfo(env, info, thisVar);
611         napi_valuetype valuetype;
612         NAPI_CALL(env, napi_typeof(env, args, &valuetype));
613         if (valuetype != napi_number) {
614             return ThrowError(env, "The type of v must be number.");
615         }
616         ProcessManager *object = nullptr;
617         NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object));
618         return object->IsAppUid(env, args);
619     }
620 
GetEnvironmentVarOfProcess(napi_env env,napi_callback_info info)621     static napi_value GetEnvironmentVarOfProcess(napi_env env, napi_callback_info info)
622     {
623         napi_value thisVar = nullptr;
624         napi_value args = GetValueFromInfo(env, info, thisVar);
625         napi_valuetype valuetype;
626         NAPI_CALL(env, napi_typeof(env, args, &valuetype));
627         if (valuetype != napi_string) {
628             return ThrowError(env, "The type of v must be string.");
629         }
630         ProcessManager *object = nullptr;
631         NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object));
632         return object->GetEnvironmentVar(env, args);
633     }
634 
ProcessManagerConstructor(napi_env env,napi_callback_info info)635     static napi_value ProcessManagerConstructor(napi_env env, napi_callback_info info)
636     {
637         napi_value thisVar = nullptr;
638         void *data = nullptr;
639         NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data));
640         auto objectInfo = new ProcessManager();
641         napi_wrap(
642             env, thisVar, objectInfo,
643             [](napi_env environment, void *data, void *hint) {
644                 auto objInfo = reinterpret_cast<ProcessManager*>(data);
645                 if (objInfo != nullptr) {
646                     delete objInfo;
647                 }
648             },
649             nullptr, nullptr);
650         return thisVar;
651     }
652 
ProcessInit(napi_env env,napi_value exports)653     static napi_value ProcessInit(napi_env env, napi_value exports)
654     {
655         const char *procssClassName = "ProcessManager";
656         napi_value processClass = nullptr;
657         napi_property_descriptor processDesc[] = {
658             DECLARE_NAPI_FUNCTION("kill", KillSigOfProcess),
659             DECLARE_NAPI_FUNCTION("exit", ExitOfProcess),
660             DECLARE_NAPI_FUNCTION("isAppUid", IsAppUidOfProcess),
661             DECLARE_NAPI_FUNCTION("getUidForName", GetUidForNameOfProcess),
662             DECLARE_NAPI_FUNCTION("getThreadPriority", GetThreadPriorityOfProcess),
663             DECLARE_NAPI_FUNCTION("getSystemConfig", GetSystemConfigOfProcess),
664             DECLARE_NAPI_FUNCTION("getEnvironmentVar", GetEnvironmentVarOfProcess),
665         };
666         NAPI_CALL(env, napi_define_class(env, procssClassName, strlen(procssClassName), ProcessManagerConstructor,
667                                          nullptr, sizeof(processDesc) / sizeof(processDesc[0]),
668                                          processDesc, &processClass));
669         napi_property_descriptor desc[] = {
670             DECLARE_NAPI_PROPERTY("ProcessManager", processClass)
671         };
672         napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
673         return exports;
674     }
Init(napi_env env,napi_value exports)675     static napi_value Init(napi_env env, napi_value exports)
676     {
677         Process object;
678         object.SetRejectionCallback(env);
679         napi_property_descriptor desc[] = {
680             DECLARE_NAPI_FUNCTION("runCmd", RunCommand),
681             DECLARE_NAPI_GETTER("uid", GetUid),
682             DECLARE_NAPI_GETTER("gid", GetGid),
683             DECLARE_NAPI_GETTER("euid", GetEUid),
684             DECLARE_NAPI_GETTER("egid", GetEGid),
685             DECLARE_NAPI_GETTER("groups", GetGroups),
686             DECLARE_NAPI_GETTER("pid", GetPid),
687             DECLARE_NAPI_GETTER("ppid", GetPpid),
688             DECLARE_NAPI_FUNCTION("uptime", Uptime),
689             DECLARE_NAPI_FUNCTION("kill", KillSig),
690             DECLARE_NAPI_FUNCTION("chdir", Chdir),
691             DECLARE_NAPI_FUNCTION("abort", Abort),
692             DECLARE_NAPI_FUNCTION("cwd", Cwd),
693             DECLARE_NAPI_FUNCTION("on", On),
694             DECLARE_NAPI_FUNCTION("off", Off),
695             DECLARE_NAPI_FUNCTION("exit", Exit),
696             DECLARE_NAPI_GETTER("tid", GetTid),
697             DECLARE_NAPI_FUNCTION("getStartRealtime", GetStartRealtime),
698             DECLARE_NAPI_FUNCTION("getPastCpuTime",  GetPastCputime),
699             DECLARE_NAPI_FUNCTION("isIsolatedProcess", IsIsolatedProcess),
700             DECLARE_NAPI_FUNCTION("is64Bit", Is64Bit),
701             DECLARE_NAPI_FUNCTION("isAppUid", IsAppUid),
702             DECLARE_NAPI_FUNCTION("getUidForName", GetUidForName),
703             DECLARE_NAPI_FUNCTION("getThreadPriority", GetThreadPriority),
704             DECLARE_NAPI_FUNCTION("getSystemConfig", GetSystemConfig),
705             DECLARE_NAPI_FUNCTION("getEnvironmentVar", GetEnvironmentVar),
706         };
707         NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
708         ProcessInit(env, exports);
709         napi_value obj = nullptr;
710         NAPI_CALL(env, napi_create_object(env, &obj));
711 
712         NAPI_CALL(env, napi_wrap(
713             env, obj, reinterpret_cast<void*>(Process::ClearReference),
714             [](napi_env env, void* data, void* hint) {
715                 if (data != nullptr) {
716                     ClearRefCallback clearParameters = reinterpret_cast<ClearRefCallback>(data);
717                     clearParameters(env);
718                 }
719             },
720             nullptr, nullptr));
721         NAPI_CALL(env, napi_set_named_property(env, exports, "obj", obj));
722 
723         return exports;
724     }
725 
726     static napi_module processModule = {
727         .nm_version = 1,
728         .nm_flags = 0,
729         .nm_filename = nullptr,
730         .nm_register_func = Init,
731         .nm_modname = "process",
732         .nm_priv = reinterpret_cast<void*>(0),
733         .reserved = { 0 },
734     };
735 
RegisterModule()736     extern "C" __attribute__ ((constructor)) void RegisterModule()
737     {
738         napi_module_register(&processModule);
739     }
740 } // namespace OHOS::Js_sys_module::Process
741