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