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