From 727fa0030e976f8f8c3aa750ead62c77602b0900 Mon Sep 17 00:00:00 2001 From: lifansheng Date: Tue, 14 Sep 2021 10:30:48 +0800 Subject: [PATCH] modify js_sys_module Signed-off-by: lifansheng --- README.en.md | 231 +++++++++++++++ README.md | 470 +++++++++++++----------------- process/js_process.cpp | 178 ++++++++++- process/js_process.h | 15 + process/native_module_process.cpp | 97 ++++++ 5 files changed, 721 insertions(+), 270 deletions(-) create mode 100755 README.en.md diff --git a/README.en.md b/README.en.md new file mode 100755 index 0000000..04b9ff3 --- /dev/null +++ b/README.en.md @@ -0,0 +1,231 @@ +#### js_sys_module +#### 一、process introduce +It is mainly to obtain the relevant ID of the process, obtain and modify the working directory of the process, and exit and close the process. Process module, involving fourteen interfaces. + +1.getUid() :number; + +The process. Getuid () method returns the digital user ID of the process. + +2.getGid() :number; + +The process. Getgid () method returns the numeric group ID of the process. + +3.getEUid() :number; + +The process. Geteuid () method returns the digitally valid user identity of the process. + +4.getEGid() :number; + +The process. Getegid () method returns the numeric valid group ID of the node. JS process. + +5.getGroups() :number[]; + +The process. Getgroups () method returns an array with supplementary group ID. + +6.getPid() :number; + +The process.pid property returns the PID of the process. + +7.getPpid() :number; + +The process.ppid property returns the PID of the parent process of the current process. + +8.chdir(dir:string) :void; + +The process. Chdir () method changes the current working directory of the node. JS process. + +9.uptime() :number; + +The process. Uptime () method returns the number of seconds that the current system has been running. + +10.Kill(pid:number, signal:number) :boolean; + +The process. Kill () method will send a signal to the process PID identified by. + +11.abort() :void; + +The process. Abort () method causes the node. JS process to exit immediately and generate a core file. + +12.on(type:string ,listener:EventListener) :void; + +The process. On () method is used to store events triggered by the user. + +13.exit(code:number):void; + +The process. Exit () method will cause the node. JS process to exit immediately. + +14.cwd():string; + +The process. Cwd () method returns the current working directory of the node. JS process. + +15.off(type:string) :boolean; + +The process. off () Clear user-stored function events. + +#### process usage method +import { Process } from '@ohos.process' export default { data: { title: "" }, + +getGid() { + var proc = new Process(); + var result = proc.getGid; + console.log("-------"+result); +}, + +getUid() { + var proc = new Process(); + var res = proc.getUid; + console.log("-------"+res); +}, + +getEgid() { + var proc = new Process(); + var resb = proc.getEgid; + console.log("-------"+resb); +}, + +getEuid() { + var proc = new Process(); + var ans = proc.getEuid; + console.log("-------"+ans); +}, + +getGroups() { + var proc = new Process(); + var answer = proc.getGroups; + console.log("-------"+answer); +}, + +uptime() { + var proc = new Process(); + var num = proc.uptime(); + console.log("---------"+num); +}, + +kill() { + var proc = new Process(); + var ansu = proc.kill(5,23); + console.log("------"+ansu); +}, + +chdir() { + var proc = new Process(); + proc.chdir("123456"); +}, + + getPid() { + var pro = new Process(); + var result = pro.getPid; + console.log("-----"+result); +}, + +getPpid(){ + var pro = new Process(); + var result = pro.getPpid; + console.log("---------"+result); +}, + +on(){ + function add(num){ + var value = num + 5; + return value; + } + var proc = new Process(); + proc.on("add",add); +}, + +off(){ + var pro = new Process(); + var result = pro.off("add"); + console.log("---------"+result); +}, + +Cwd(){ + var pro = new Process(); + var result = pro.cwd(); + console.log("----"+result); +}, + +exit(){ + var pro = new Process(); + pro.exit(15); +}, + +abort(){ + var pro = new Process(); + pro.abort(); +}, + +onInit() { + this.title = "strings.world"; +} +} + +#### 二、childprocess introduction + +The childprocess object can be used to create a new process. The main process can obtain the standard input and output of the child process, + +send signals and close the child process. + +Interface introduction + +1.runCmd(command : string, options?: RunOptions): ChildProcess + +Runcmd can fork a new process to run a shell and return the childprocess object. + +The first parameter command refers to the shell to be run, and the second parameter options refers to some running parameters of the child + +process.These parameters mainly refer to timeout, killsignal and maxbuffer. + +If timeout is set, the child process will send a signal killsignal after timeout is exceeded. Maxbuffer is used to limit the maximum stdout and + +stderr sizes that can be received. + +2.wait(): Promise + +The wait function is used to wait for the child process to run and return the promise object, whose value is the exit code of the child process. + +3.getOutput(): Promise + +The getoutput function is used to get the standard output of the child process. + +4.getErrorOutput(): Promise + +The geterroroutput function is used to obtain the standard error output of the child process. + +5.close(): void + +The close function is used to close the running child process. + +6.kill(signo: number): void + +The kill function is used to send signals to child processes. + +7.readonly killed: boolean + +Killed indicates whether the signal is sent successfully. + +8.readonly exitCode: number + +Exitcode indicates the exit code of the child process. + +9.pid和ppid + +Represents the child process ID and the main process ID respectively. + +#### usage method +Take the LS command as an example + + var child = childprocess.runCmd("ls", {maxBuffer:9999,killSignal:15} ); + var stdoutRes = child.getOutput(); + var stderrRes = child.getErrorOutput(); + var status = child.wait(); + stdoutRes.then(val=>{ + console.log("stdout = :" + val); + }); + stderrRes.then(val=>{ + console.log("stderr = :" + val); + }); + status.then(val=>{ + console.log("status = :" + val); + }); + diff --git a/README.md b/README.md index f6f87d0..0d1d921 100755 --- a/README.md +++ b/README.md @@ -1,292 +1,226 @@ -# js_sys_module子系统/组件 +#### js_sys_module +#### 一、process 介绍 +主要是获取进程的相关id以及获取和修改进程的工作目录,及进程的退出关闭。 process进程模块,涉及14个接口。 -- [简介](#简介) -- [目录](#目录) -- [说明](#说明) - - [接口说明](#接口说明) - - [使用说明](#使用说明) +1.getUid() :number; -- [相关仓](#相关仓) +该process.getuid()方法返回进程的数字用户标识。 -## 简介 -process主要是获取进程的相关id以及获取和修改进程的工作目录,及进程的退出关闭。通过childprocess对象可以用来创建一个新的进程,主进程可以获取子进程的标准输入输出,以及发送信号和关闭子进程。 -## 目录 +2.getGid() :number; -``` -base/compileruntime/js_sys_module/ -├── Class:PROCESS # PROCESS类 -├── Uid # Uid属性 -├── Gid # Gid属性 -├── EUid # EUid属性 -├── EGid # EGid属性 -├── Groups # Groups属性 -├── Pid # Pid属性 -├── Ppid # Ppid属性 -├── chdir() # chdir方法 -├── uptime() # uptime方法 -├── kill() # kill方法 -├── abort() # abort方法 -├── on() # on方法 -├── exit() # exit方法 -├── cwd() # cwd方法 -├── off() # off方法 -├── runCmd() # runCmd方法 -└─── Class:CHILDPROCESS # CHILDPROCESS类 - ├── close() # close方法 - ├── kill() # kill方法 - ├── getOutput() # getOutput方法 - ├── getErrorOutput() # getErrorOutput方法 - ├── wait() # wait方法 - ├── killed # killed属性 - ├── pid # pid属性 - ├── ppid # ppid属性 - └── exitCode # exitCode属性 -``` +该process.getgid()方法返回进程的数字组标识。 -## 说明 +3.getEUid() :number; -### 接口说明 -| 接口名 | 说明 | -| -------- | -------- | -| getUid() :number | 返回进程的数字用户标识。 | -| getGid() :number | 返回进程的数字组标识。 | -| getEUid() :number | 返回进程的数字有效用户身份。 | -| getEGid() :number | 返回 Node.js 进程的数字有效组标识。 | -| getGroups() :number[] | 返回一个带有补充组 ID 的数组。 | -| getPid() :number | 返回进程的 PID。 | -| getPpid() :number | 返回当前进程的父进程的 PID。 | -| chdir(dir:string) :void | 更改 Node.js 进程的当前工作目录。 | -| uptime() :number | 返回当前系统已运行的秒数。 | -| Kill(pid:number, signal:number) :boolean | 将signal信号发送到标识的进程 PID,true代表发送成功。 | -| abort() :void | 会导致 Node.js 进程立即退出并生成一个核心文件。 | -| on(type:string ,listener:EventListener) :void | 用来存储用户所触发的事件。 | -| exit(code:number):void | 会导致 Node.js 进程立即退出。 | -| cwd():string | 返回 Node.js 进程的当前工作目录。 | -| off(type: string): boolean | 会清除用户存储的事件,true代表清除成功。 | -| runCmd(command : string, options?: RunOptions): ChildProcess | 通过runcmd可以fork一个新的进程来运行一段shell,并返回ChildProcess对象。第一个参数command指需要运行的shell,第二个参数options指子进程的一些运行参数。这些参数主要指timeout、killSignal、maxBuffer 。如果设置了timeout则子进程会在超出timeout后发送信号killSignal,maxBuffer用来限制可接收的最大stdout和stderr大小。 | -| wait(): Promise | 用来等待子进程运行结束,返回promise对象,其值为子进程的退出码。 | -| getOutput(): Promise | 用来获取子进程的标准输出。 | -| getErrorOutput(): Promise | 用来获取子进程的标准错误输出。 | -| close(): void | 用来关闭正在运行的子进程。 | -| kill(signo: number): void | 用来发送信号给子进程。 | -| readonly killed: boolean | 表示信号是否发送成功,true代表发送成功。 | -| readonly exitCode: number | 表示子进程的退出码。 | -| pid | 代表子进程ID。 | -| ppid | 代表主进程ID。 | +该process.geteuid()方法返回进程的数字有效用户身份。 -### 使用说明 +4.getEGid() :number; -各接口使用方法如下: -1.getUid() -``` -getUid(){ - var res = Process.getUid; - console.log("-------"+res); -} -``` -2.getGid() -``` -getGid(){ - var result = Process.getGid; +该process.getegid()方法返回 Node.js 进程的数字有效组标识。 + +5.getGroups() :number[]; + +该process.getgroups()方法返回一个带有补充组 ID 的数组。 + +6.getPid() :number; + +该process.pid属性返回进程的 PID。 + +7.getPpid() :number; + +该process.ppid属性返回当前进程的父进程的 PID。 + +8.chdir(dir:string) :void; + +该process.chdir()方法更改 Node.js 进程的当前工作目录。 + +9.uptime() :number; + +该process.uptime()方法返回当前系统已运行的秒数。 + +10.Kill(pid:number, signal:number) :boolean; + +该process.kill()方法将 发送signal到由 标识的进程 pid。 + +11.abort() :void; + +该process.abort()方法会导致 Node.js 进程立即退出并生成一个核心文件。 + +12.on(type:string ,listener:EventListener) :void; + +该process.on()方法是用来存储用户所触发的事件。 + +13.exit(code:number):void; + +该process.Exit()方法会导致 Node.js 进程立即退出。 + +14.cwd():string; + +该process.cwd()方法返回 Node.js 进程的当前工作目录。 + +15.off(type: string): boolean; + +该process.off()方法会清除用户存储的事件。 + +process 使用方法 +import { Process } from '@ohos.process' export default { data: { title: "" }, + +getGid() { + var proc = new Process(); + var result = proc.getGid; console.log("-------"+result); -} -``` -3.getEuid() -``` -getEuid(){ - var ans = Process.getEuid; - console.log("-------"+ans); -} -``` -4.getEgid() -``` -getEgid(){ - var resb = Process.getEgid; +}, + +getUid() { + var proc = new Process(); + var res = proc.getUid; + console.log("-------"+res); +}, + +getEgid() { + var proc = new Process(); + var resb = proc.getEgid; console.log("-------"+resb); -} -``` -5.getGroups() -``` -getGroups(){ - var answer = Process.getGroups; +}, + +getEuid() { + var proc = new Process(); + var ans = proc.getEuid; + console.log("-------"+ans); +}, + +getGroups() { + var proc = new Process(); + var answer = proc.getGroups; console.log("-------"+answer); -} -``` -6.getPid() -``` -getPid(){ - var result = Process.getPid; - console.log("-----"+result); -} -``` -7.getPpid() -``` -getPpid(){ - var result = Process.getPpid; - console.log("---------"+result); -} -``` -8.chdir() -``` -chdir(){ - Process.chdir("123456"); -} -``` -9.uptime() -``` -uptime(){ - var num = Process.uptime(); +}, + +uptime() { + var proc = new Process(); + var num = proc.uptime(); console.log("---------"+num); -} -``` -10.kill() -``` -kill(){ - var ansu = Process.kill(5,23); +}, + +kill() { + var proc = new Process(); + var ansu = proc.kill(5,10); console.log("------"+ansu); -} -``` -11.abort() -``` -abort(){ - Process.abort(); -} -``` -12.on() -``` +}, + +chdir() { + var proc = new Process(); + proc.chdir("123456"); +}, + + getPid() { + var pro = new Process(); + var result = pro.getPid; + console.log("-----"+result); +}, + +getPpid(){ + var pro = new Process(); + var result = pro.getPpid; + console.log("---------"+result); +}, + on(){ function add(num){ var value = num + 5; return value; } - Process.on("add",add); -} -``` -13.exit() -``` -exit(){ - Process.exit(15); -} -``` -14.Cwd() -``` -Cwd(){ - var result = Process.cwd(); - console.log("----"+result); -} -``` -15.off() + var proc = new Process(); + proc.on("add",add); +}, -``` off(){ - var result = Process.off("add"); - console.log("---------"+result); -} -``` -16.runCmd() -``` -runCmd(){ - var child = process.runCmd('echo abc') - //killSignal可以是数字或字符串 - var child = process.runCmd('echo abc;', {killSignal : 'SIGKILL'}); - var child = process.runCmd('sleep 5; echo abc;', {timeout : 1, killSignal : 9, maxBuffer : 2}) -} -``` -17.wait() -``` -wait() -{ - var child = process.runCmd('ls') - var status = child.wait(); - status.then(val => { - console.log(val); - }) -} -``` -18.getOutput() -``` -getOutput(){ - var child = process.runCmd('echo bcd;'); - var res = child.getOutput(); - child.wait(); - res.then(val => { - console.log(val); - }) -} -``` -19.getErrorOutput() -``` -getErrorOutput(){ - var child = process.runCmd('makdir 1.txt'); //执行一个错误命令 - var res = child.getErrorOutput(); - child.wait(); - res.then(val => { - console.log(val); - }) -} -``` -20.close() -``` -close(){ - var child = process.runCmd('ls; sleep 5s;') - var result = child.close() - console.log(child.exitCode); -} -``` -21.kill() -``` -kill(){ - var child = process.runCmd('ls; sleep 5s;') - var result = child.kill('SIGHUP'); - child.wait(); - var temp = child.killed; - console.log(temp); -} -``` -22.killed -``` -{ - var child = process.runCmd('ls; sleep 5;') - child.kill(3); - var killed_ = child.killed; - console.log(killed_); - child.wait(); -} -``` -23.exitCode -``` -{ - var child = process.runCmd('ls; sleep 5;') - child.kill(9); - child.wait(); - var exitCode_ = child.exitCode; - console.log(exitCode_); -} -``` -24.pid -``` -pid -{ - var child = process.runCmd('ls; sleep 5;') - var pid_ = child.pid; - console.log(pid_); - child.wait(); -} -``` -25.ppid -``` -ppid -{ - var child = process.runCmd('ls; sleep 5;') - var ppid_ = child.ppid; - console.log(ppid_); - child.wait(); -} -``` + var pro = new Process(); + var result = pro.off("add"); + console.log("--------"+result); +}, +Cwd(){ + var pro = new Process(); + var result = pro.cwd(); + console.log("----"+result); +}, -## 相关仓 +exit(){ + var pro = new Process(); + pro.exit(15); +}, -[js_sys_module子系统](https://gitee.com/OHOS_STD/js_sys_module) +abort(){ + var pro = new Process(); + pro.abort(); +}, -[base/compileruntime/js_sys_module/](base/compileruntime/js_sys_module-readme.md) +onInit() { + this.title = "strings.world"; +} +} + +#### 二、childprocess简介 + +通过childprocess对象可以用来创建一个新的进程,主进程可以获取子进程的标准输入输出,以及发送信号和关闭子进程。 + +接口介绍 + +1.runCmd(command : string, options?: RunOptions): ChildProcess + +通过runcmd可以fork一个新的进程来运行一段shell,并返回ChildProcess对象。 + +第一个参数command指需要运行的shell,第二个参数options指子进程的一些运行参数。 + +这些参数主要指timeout、killSignal、maxBuffer 。 + +如果设置了timeout则子进程会在超出timeout后发送信号killSignal,maxBuffer用来限制可接收的最大stdout和stderr大小。 + +2.wait(): Promise + +wait函数用来等待子进程运行结束,返回promise对象,其值为子进程的退出码。 + +3.getOutput(): Promise + +getOutput函数用来获取子进程的标准输出。 + +4.getErrorOutput(): Promise + +getErrorOutput函数用来获取子进程的标准错误输出。 + +5.close(): void + +close函数用来关闭正在运行的子进程。 + +6.kill(signo: number): void + +kill函数用来发送信号给子进程。 + +7.readonly killed: boolean + +killed表示信号是否发送成功。 + +8.readonly exitCode: number + +exitCode表示子进程的退出吗 + +9.pid和ppid + +分别代表子进程id和主进程id + +使用方法 +以ls命令为例 + + var child = childprocess.runCmd("ls", {maxBuffer:9999,killSignal:15} ); + var stdoutRes = child.getOutput(); + var stderrRes = child.getErrorOutput(); + var status = child.wait(); + stdoutRes.then(val=>{ + console.log("stdout = :" + val); + }); + stderrRes.then(val=>{ + console.log("stderr = :" + val); + }); + status.then(val=>{ + console.log("status = :" + val); + }); \ No newline at end of file diff --git a/process/js_process.cpp b/process/js_process.cpp index 4b6a424..ae198a2 100644 --- a/process/js_process.cpp +++ b/process/js_process.cpp @@ -21,17 +21,22 @@ #include #include #include +#include +#include #include #include +#include +#include #include +#include #include "securec.h" #include "utils/log.h" namespace OHOS::Js_sys_module::Process { namespace { constexpr int NUM_OF_DATA = 4; - constexpr int MAX_PATH = 260; + constexpr int PER_USER_RANGE = 100000; } std::map Process::m_map_process_event_; Process::Process(napi_env env_) : env(env_) {} @@ -190,7 +195,7 @@ namespace OHOS::Js_sys_module::Process { napi_value Process::Cwd() const { napi_value result = nullptr; - char buf[MAX_PATH * NUM_OF_DATA] = { 0 }; + char buf[260 * NUM_OF_DATA] = { 0 }; // 260:Only numbers path String size is 260. size_t length = sizeof(buf); int err = uv_cwd(buf, &length); if (err) { @@ -220,6 +225,7 @@ namespace OHOS::Js_sys_module::Process { buffer = nullptr; } } + napi_value Process::Off(napi_value str) { char *buffer = nullptr; @@ -248,4 +254,172 @@ namespace OHOS::Js_sys_module::Process { NAPI_CALL(env, napi_get_boolean(env, flag, &result)); return result; } + + napi_value Process::GetTid() const + { + napi_value result = nullptr; + auto proTid = static_cast(gettid()); + napi_create_int32(env, proTid, &result); + return result; + } + + napi_value Process::IsIsolatedProcess() const + { + napi_value result = nullptr; + bool flag = true; + auto prouid = static_cast(getuid()); + auto uid = prouid % PER_USER_RANGE; + if ((uid >= 99000 && uid <= 99999) || // 99999:Only isolateuid numbers between 99000 and 99999. + (uid >= 9000 && uid <= 98999)) { // 98999:Only appuid numbers betweeen 9000 and 98999. + NAPI_CALL(env, napi_get_boolean(env, flag, &result)); + return result; + } + flag = false; + NAPI_CALL(env, napi_get_boolean(env, flag, &result)); + return result; + } + + napi_value Process::IsAppUid(napi_value uid) const + { + int32_t number = 0; + napi_value result = nullptr; + bool flag = true; + napi_get_value_int32(env, uid, &number); + if (number > 0) { + const auto appId = number % PER_USER_RANGE; + if (appId >= FIRST_APPLICATION_UID && appId <= LAST_APPLICATION_UID) { + napi_get_boolean(env, flag, &result); + return result; + } + flag = false; + NAPI_CALL(env, napi_get_boolean(env, flag, &result)); + return result; + } else { + flag = false; + NAPI_CALL(env, napi_get_boolean(env, flag, &result)); + return result; + } + } + + napi_value Process::Is64Bit() const + { + napi_value result = nullptr; + bool flag = true; + int32_t* number = nullptr; + auto size = sizeof(number); + if (size == NUM_OF_DATA) { + flag = false; + NAPI_CALL(env, napi_get_boolean(env, flag, &result)); + return result; + } else { + NAPI_CALL(env, napi_get_boolean(env, flag, &result)); + return result; + } + } + + napi_value Process::GetUidForName(napi_value name) const + { + struct passwd *user; + int32_t uid = 0; + napi_value result = nullptr; + char *buffer = nullptr; + size_t bufferSize = 0; + napi_get_value_string_utf8(env, name, buffer, 0, &bufferSize); + if (bufferSize > 0) { + buffer = new char[bufferSize + 1]; + } + napi_get_value_string_utf8(env, name, buffer, bufferSize + 1, &bufferSize); + std::string temp = ""; + if (buffer != nullptr) { + temp = buffer; + delete []buffer; + buffer = nullptr; + } + user = getpwnam(temp.c_str()); + if (user != nullptr) { + uid = static_cast(user->pw_uid); + napi_create_int32(env, uid, &result); + return result; + } + napi_create_int32(env, (-1), &result); + return result; + } + + napi_value Process::GetThreadPriority(napi_value tid) const + { + errno = 0; + int32_t proTid = 0; + napi_value result = nullptr; + napi_get_value_int32(env, tid, &proTid); + int32_t pri = getpriority(PRIO_PROCESS, proTid); + if (errno != 0) { + napi_throw_error(env, "-1", "Invalid tid"); + } + napi_create_int32(env, pri, &result); + return result; + } + + napi_value Process::GetStartRealtime() const + { + struct timespec timespro; + struct timespec timessys; + napi_value result = nullptr; + auto res = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, ×pro); + if (res != 0) { + return 0; + } + auto res1 = clock_gettime(CLOCK_MONOTONIC, ×sys); + if (res1 != 0) { + return 0; + } + double whenpro = ConvertTime(timespro.tv_sec, timespro.tv_nsec); + double whensys = ConvertTime(timessys.tv_sec, timessys.tv_nsec); + auto timedif = (whensys - whenpro); + napi_create_double(env, timedif, &result); + return result; + } + + napi_value Process::GetAvailableCores() const + { + napi_value result = nullptr; + auto num_cpus = static_cast(sysconf(_SC_NPROCESSORS_ONLN)); + NAPI_CALL(env, napi_create_array(env, &result)); + for (int i = 0; i <= num_cpus; i++) { + napi_value numvalue = nullptr; + napi_create_uint32(env, i, &numvalue); + napi_status status = napi_set_element(env, result, i, numvalue); + if (status != napi_ok) { + HILOG_INFO("set element error"); + } + } + return result; + } + + double Process::ConvertTime(time_t tvsec, long tvnsec) const + { + return double(tvsec * 1000) + double(tvnsec / 1000000); // 98999:Only converttime numbers is 1000 and 1000000. + } + + napi_value Process::GetPastCputime() const + { + struct timespec times; + napi_value result = nullptr; + auto res = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, ×); + if (res != 0) { + return 0; + } + double when = ConvertTime(times.tv_sec, times.tv_nsec); + napi_create_double(env, when, &result); + return result; + } + + napi_value Process::GetSystemConfig(napi_value name) + { + int32_t number = 0; + napi_value result = nullptr; + napi_get_value_int32(env, name, &number); + auto configinfo = static_cast(sysconf(number)); + napi_create_int32(env, configinfo, &result); + return result; + } } \ No newline at end of file diff --git a/process/js_process.h b/process/js_process.h index 116fa9b..3a02542 100644 --- a/process/js_process.h +++ b/process/js_process.h @@ -18,6 +18,7 @@ #include #include +#include #include "napi/native_api.h" #include "napi/native_node_api.h" @@ -41,8 +42,22 @@ namespace OHOS::Js_sys_module::Process { napi_value Off(napi_value str); void Exit(napi_value number) const; napi_value Cwd() const; + napi_value GetTid() const; + napi_value IsIsolatedProcess() const; + napi_value IsAppUid(napi_value uid) const; + napi_value Is64Bit() const; + napi_value GetUidForName(napi_value name) const; + napi_value GetThreadPriority(napi_value tid) const; + napi_value GetStartRealtime() const; + napi_value GetPastCputime() const; + napi_value GetSystemConfig(napi_value name); + napi_value GetAvailableCores() const; + private: + double ConvertTime(time_t tvsec, long tvnsec) const; private: napi_env env; + int FIRST_APPLICATION_UID = 10000; + int LAST_APPLICATION_UID = 19999; static std::map m_map_process_event_; }; } diff --git a/process/native_module_process.cpp b/process/native_module_process.cpp index 4c0126f..e4bd728 100644 --- a/process/native_module_process.cpp +++ b/process/native_module_process.cpp @@ -373,6 +373,93 @@ namespace OHOS::Js_sys_module::Process { result = object.Kill(argv[0], argv[1]); return result; } + static napi_value GetTid(napi_env env, napi_callback_info info) + { + Process object(env); + return object.GetTid(); + } + + static napi_value IsIsolatedProcess(napi_env env, napi_callback_info info) + { + Process object(env); + return object.IsIsolatedProcess(); + } + + static napi_value IsAppUid(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + size_t argc = 1; + napi_value args = nullptr; + napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr); + napi_valuetype valuetype; + NAPI_CALL(env, napi_typeof(env, args, &valuetype)); + NAPI_ASSERT(env, valuetype == napi_number, "Wrong argument type.String error"); + Process object(env); + return object.IsAppUid(args); + } + + static napi_value Is64Bit(napi_env env, napi_callback_info info) + { + Process object(env); + return object.Is64Bit(); + } + + static napi_value GetUidForName(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + size_t argc = 1; + napi_value args = nullptr; + napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr); + napi_valuetype valuetype; + NAPI_CALL(env, napi_typeof(env, args, &valuetype)); + NAPI_ASSERT(env, valuetype == napi_string, "Wrong argument type.String error"); + Process object(env); + return object.GetUidForName(args); + } + + static napi_value GetThreadPriority(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + size_t argc = 1; + napi_value args = nullptr; + napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr); + napi_valuetype valuetype; + NAPI_CALL(env, napi_typeof(env, args, &valuetype)); + NAPI_ASSERT(env, valuetype == napi_number, "Wrong argument type.String error"); + Process object(env); + return object.GetThreadPriority(args); + } + + static napi_value GetStartRealtime(napi_env env, napi_callback_info info) + { + Process object(env); + return object.GetStartRealtime(); + } + + static napi_value GetPastCputime(napi_env env, napi_callback_info info) + { + Process object(env); + return object.GetPastCputime(); + } + + static napi_value GetSystemConfig(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + size_t argc = 1; + napi_value args = nullptr; + napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr); + napi_valuetype valuetype; + NAPI_CALL(env, napi_typeof(env, args, &valuetype)); + NAPI_ASSERT(env, valuetype == napi_number, "Wrong argument type.String error"); + Process object(env); + return object.GetSystemConfig(args); + } + + static napi_value GetAvailableCores(napi_env env, napi_callback_info info) + { + Process object(env); + return object.GetAvailableCores(); + } static napi_value Init(napi_env env, napi_value exports) { @@ -393,6 +480,16 @@ namespace OHOS::Js_sys_module::Process { DECLARE_NAPI_FUNCTION("on", On), DECLARE_NAPI_FUNCTION("off", Off), DECLARE_NAPI_FUNCTION("exit", Exit), + DECLARE_NAPI_FUNCTION("getTid", GetTid), + DECLARE_NAPI_FUNCTION("getStartRealtime", GetStartRealtime), + DECLARE_NAPI_FUNCTION("getAvailableCores", GetAvailableCores), + DECLARE_NAPI_FUNCTION("getPastCputime", GetPastCputime), + DECLARE_NAPI_FUNCTION("isIsolatedProcess", IsIsolatedProcess), + DECLARE_NAPI_FUNCTION("is64Bit", Is64Bit), + DECLARE_NAPI_FUNCTION("isAppUid", IsAppUid), + DECLARE_NAPI_FUNCTION("getUidForName", GetUidForName), + DECLARE_NAPI_FUNCTION("getThreadPriority", GetThreadPriority), + DECLARE_NAPI_FUNCTION("getSystemConfig", GetSystemConfig), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); return exports;