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
26namespace OHOS::JsSysModule::Process {
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
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
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
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
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
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
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
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
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
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
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
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
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
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    }
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
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
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
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
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
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
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
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
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
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
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    }
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    }
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
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
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
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
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
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_wrap(
645            env, thisVar, objectInfo,
646            [](napi_env environment, void *data, void *hint) {
647                auto objInfo = reinterpret_cast<ProcessManager*>(data);
648                if (objInfo != nullptr) {
649                    delete objInfo;
650                }
651            },
652            nullptr, nullptr);
653        return thisVar;
654    }
655
656    static napi_value ProcessInit(napi_env env, napi_value exports)
657    {
658        const char *procssClassName = "ProcessManager";
659        napi_value processClass = nullptr;
660        napi_property_descriptor processDesc[] = {
661            DECLARE_NAPI_FUNCTION("kill", KillSigOfProcess),
662            DECLARE_NAPI_FUNCTION("exit", ExitOfProcess),
663            DECLARE_NAPI_FUNCTION("isAppUid", IsAppUidOfProcess),
664            DECLARE_NAPI_FUNCTION("getUidForName", GetUidForNameOfProcess),
665            DECLARE_NAPI_FUNCTION("getThreadPriority", GetThreadPriorityOfProcess),
666            DECLARE_NAPI_FUNCTION("getSystemConfig", GetSystemConfigOfProcess),
667            DECLARE_NAPI_FUNCTION("getEnvironmentVar", GetEnvironmentVarOfProcess),
668        };
669        NAPI_CALL(env, napi_define_class(env, procssClassName, strlen(procssClassName), ProcessManagerConstructor,
670                                         nullptr, sizeof(processDesc) / sizeof(processDesc[0]),
671                                         processDesc, &processClass));
672        napi_property_descriptor desc[] = {
673            DECLARE_NAPI_PROPERTY("ProcessManager", processClass)
674        };
675        napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
676        return exports;
677    }
678    static napi_value Init(napi_env env, napi_value exports)
679    {
680        Process object;
681        napi_property_descriptor desc[] = {
682            DECLARE_NAPI_FUNCTION("runCmd", RunCommand),
683            DECLARE_NAPI_GETTER("uid", GetUid),
684            DECLARE_NAPI_GETTER("gid", GetGid),
685            DECLARE_NAPI_GETTER("euid", GetEUid),
686            DECLARE_NAPI_GETTER("egid", GetEGid),
687            DECLARE_NAPI_GETTER("groups", GetGroups),
688            DECLARE_NAPI_GETTER("pid", GetPid),
689            DECLARE_NAPI_GETTER("ppid", GetPpid),
690            DECLARE_NAPI_FUNCTION("uptime", Uptime),
691            DECLARE_NAPI_FUNCTION("kill", KillSig),
692            DECLARE_NAPI_FUNCTION("chdir", Chdir),
693            DECLARE_NAPI_FUNCTION("abort", Abort),
694            DECLARE_NAPI_FUNCTION("cwd", Cwd),
695            DECLARE_NAPI_FUNCTION("on", On),
696            DECLARE_NAPI_FUNCTION("off", Off),
697            DECLARE_NAPI_FUNCTION("exit", Exit),
698            DECLARE_NAPI_GETTER("tid", GetTid),
699            DECLARE_NAPI_FUNCTION("getStartRealtime", GetStartRealtime),
700            DECLARE_NAPI_FUNCTION("getPastCpuTime",  GetPastCputime),
701            DECLARE_NAPI_FUNCTION("isIsolatedProcess", IsIsolatedProcess),
702            DECLARE_NAPI_FUNCTION("is64Bit", Is64Bit),
703            DECLARE_NAPI_FUNCTION("isAppUid", IsAppUid),
704            DECLARE_NAPI_FUNCTION("getUidForName", GetUidForName),
705            DECLARE_NAPI_FUNCTION("getThreadPriority", GetThreadPriority),
706            DECLARE_NAPI_FUNCTION("getSystemConfig", GetSystemConfig),
707            DECLARE_NAPI_FUNCTION("getEnvironmentVar", GetEnvironmentVar),
708        };
709        NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
710        ProcessInit(env, exports);
711        napi_value obj = nullptr;
712        NAPI_CALL(env, napi_create_object(env, &obj));
713
714        NAPI_CALL(env, napi_wrap(
715            env, obj, reinterpret_cast<void*>(Process::ClearReference),
716            [](napi_env env, void* data, void* hint) {
717                if (data != nullptr) {
718                    ClearRefCallback clearParameters = reinterpret_cast<ClearRefCallback>(data);
719                    clearParameters(env);
720                }
721            },
722            nullptr, nullptr));
723        NAPI_CALL(env, napi_set_named_property(env, exports, "obj", obj));
724
725        return exports;
726    }
727
728    static napi_module processModule = {
729        .nm_version = 1,
730        .nm_flags = 0,
731        .nm_filename = nullptr,
732        .nm_register_func = Init,
733        .nm_modname = "process",
734        .nm_priv = reinterpret_cast<void*>(0),
735        .reserved = { 0 },
736    };
737
738    extern "C" __attribute__ ((constructor)) void ProcessRegisterModule()
739    {
740        napi_module_register(&processModule);
741    }
742} // namespace OHOS::JsSysModule::Process
743