diff --git a/dfx/native_module_dfx.cpp b/dfx/native_module_dfx.cpp index af19288..69f2eae 100644 --- a/dfx/native_module_dfx.cpp +++ b/dfx/native_module_dfx.cpp @@ -122,7 +122,7 @@ namespace OHOS::Js_sys_module::Dfx { NAPI_CALL(env, napi_get_boolean(env, stopResult, &result)); return result; } - + static napi_value PrintStatisticResult(napi_env env, napi_callback_info info) { NativeEngine *engine = reinterpret_cast(env); @@ -131,7 +131,7 @@ namespace OHOS::Js_sys_module::Dfx { NAPI_CALL(env, napi_get_undefined(env, &result)); return result; } - + static napi_value StartRuntimeStat(napi_env env, napi_callback_info info) { NativeEngine *engine = reinterpret_cast(env); @@ -140,7 +140,7 @@ namespace OHOS::Js_sys_module::Dfx { NAPI_CALL(env, napi_get_undefined(env, &result)); return result; } - + static napi_value StopRuntimeStat(napi_env env, napi_callback_info info) { NativeEngine *engine = reinterpret_cast(env); @@ -149,7 +149,7 @@ namespace OHOS::Js_sys_module::Dfx { NAPI_CALL(env, napi_get_undefined(env, &result)); return result; } - + static napi_value GetArrayBufferSize(napi_env env, napi_callback_info info) { NativeEngine *engine = reinterpret_cast(env); @@ -158,7 +158,7 @@ namespace OHOS::Js_sys_module::Dfx { NAPI_CALL(env, napi_create_uint32(env, value, &result)); return result; } - + static napi_value GetHeapTotalSize(napi_env env, napi_callback_info info) { NativeEngine *engine = reinterpret_cast(env); @@ -167,7 +167,7 @@ namespace OHOS::Js_sys_module::Dfx { NAPI_CALL(env, napi_create_uint32(env, value, &result)); return result; } - + static napi_value GetHeapUsedSize(napi_env env, napi_callback_info info) { NativeEngine *engine = reinterpret_cast(env); @@ -194,6 +194,7 @@ namespace OHOS::Js_sys_module::Dfx { NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); return exports; } + // dfx module define static napi_module dfxModule = { .nm_version = 1, @@ -201,7 +202,7 @@ namespace OHOS::Js_sys_module::Dfx { .nm_filename = nullptr, .nm_register_func = DfxInit, .nm_modname = "dfx", - .nm_priv = ((void*)0), + .nm_priv = reinterpret_cast(0), .reserved = {0}, }; @@ -211,4 +212,4 @@ namespace OHOS::Js_sys_module::Dfx { { napi_module_register(&dfxModule); } -} \ No newline at end of file +} // namespace OHOS::Js_sys_module::Dfx diff --git a/process/js_childprocess.cpp b/process/js_childprocess.cpp index f66cb07..368af1b 100644 --- a/process/js_childprocess.cpp +++ b/process/js_childprocess.cpp @@ -90,6 +90,10 @@ namespace OHOS::Js_sys_module::Process { exit(127); // 127:The parameter value } } else if (pid > 0) { + if (optionsInfo_ == nullptr) { + HILOG_ERROR("optionsInfo_ is nullptr"); + return; + } optionsInfo_->pid = pid; ppid_ = getpid(); CreateWorker(); @@ -121,6 +125,12 @@ namespace OHOS::Js_sys_module::Process { if (isWait_) { int32_t status; isWait_ = false; + if (optionsInfo_ == nullptr) { + napi_value res = nullptr; + NAPI_CALL(env_, napi_get_undefined(env_, &res)); + HILOG_ERROR("optionsInfo_ is nullptr"); + return res; + } waitpid(optionsInfo_->pid, &status, 0); exitCode_ = status; } @@ -136,6 +146,12 @@ namespace OHOS::Js_sys_module::Process { napi_value ChildProcess::GetOutput() const { + if (stdOutInfo_ == nullptr) { + napi_value res = nullptr; + NAPI_CALL(env_, napi_get_undefined(env_, &res)); + HILOG_ERROR("stdOutInfo_ is nullptr"); + return res; + } NAPI_CALL(env_, napi_create_promise(env_, &stdOutInfo_->deferred, &stdOutInfo_->promise)); void* data = nullptr; napi_value arrayBuffer = nullptr; @@ -158,6 +174,12 @@ namespace OHOS::Js_sys_module::Process { napi_value ChildProcess::GetErrorOutput() const { + if (stdErrInfo_ == nullptr) { + napi_value res = nullptr; + NAPI_CALL(env_, napi_get_undefined(env_, &res)); + HILOG_ERROR("stdErrInfo_ is nullptr"); + return res; + } NAPI_CALL(env_, napi_create_promise(env_, &stdErrInfo_->deferred, &stdErrInfo_->promise)); void* data = nullptr; napi_value arrayBuffer = nullptr; @@ -189,6 +211,12 @@ namespace OHOS::Js_sys_module::Process { napi_value ChildProcess::Getpid() const { napi_value result = nullptr; + if (optionsInfo_ == nullptr) { + napi_value res = nullptr; + NAPI_CALL(env_, napi_get_undefined(env_, &res)); + HILOG_ERROR("optionsInfo_ is nullptr"); + return res; + } NAPI_CALL(env_, napi_create_int32(env_, optionsInfo_->pid, &result)); return result; @@ -215,8 +243,16 @@ namespace OHOS::Js_sys_module::Process { // getstdout napi_value resourceName = nullptr; stdOutInfo_ = new StdInfo(); + if (stdOutInfo_ == nullptr) { + HILOG_ERROR("stdOutInfo_ is nullptr"); + return; + } stdOutInfo_->isNeedRun = &isNeedRun_; stdOutInfo_->fd = stdOutFd_[0]; + if (optionsInfo_ == nullptr) { + HILOG_ERROR("optionsInfo_ is nullptr"); + return; + } stdOutInfo_->pid = optionsInfo_->pid; stdOutInfo_->maxBuffSize = optionsInfo_->maxBuffer; napi_create_string_utf8(env_, "ReadStdOut", NAPI_AUTO_LENGTH, &resourceName); @@ -226,6 +262,10 @@ namespace OHOS::Js_sys_module::Process { // getstderr stdErrInfo_ = new StdInfo(); + if (stdErrInfo_ == nullptr) { + HILOG_ERROR("stdErrInfo_ is nullptr"); + return; + } stdErrInfo_->isNeedRun = &isNeedRun_; stdErrInfo_->fd = stdErrFd_[0]; stdErrInfo_->pid = optionsInfo_->pid; @@ -332,6 +372,10 @@ namespace OHOS::Js_sys_module::Process { { int signal = GetValidSignal(signo); std::vector signalType = {SIGINT, SIGQUIT, SIGKILL, SIGTERM}; + if (optionsInfo_ == nullptr) { + HILOG_ERROR("optionsInfo_ is nullptr"); + return; + } if (!kill(optionsInfo_->pid, signal)) { auto res = std::find(signalType.begin(), signalType.end(), static_cast(signal)); (res != signalType.end()) ? isNeedRun_ = false : 0; @@ -344,6 +388,10 @@ namespace OHOS::Js_sys_module::Process { void ChildProcess::Close() { int32_t status = 0; + if (optionsInfo_ == nullptr) { + HILOG_ERROR("optionsInfo_ is nullptr"); + return; + } if (isWait_ && !(waitpid(optionsInfo_->pid, &status, WNOHANG)) && isNeedRun_) { if (!kill(optionsInfo_->pid, SIGKILL)) { waitpid(optionsInfo_->pid, &status, 0); @@ -378,6 +426,10 @@ namespace OHOS::Js_sys_module::Process { { std::vector keyStr = {"timeout", "killSignal", "maxBuffer"}; optionsInfo_ = new OptionsInfo(); + if (optionsInfo_ == nullptr) { + HILOG_ERROR("optionsInfo_ is nullptr"); + return; + } size_t size = keyStr.size(); for (size_t i = 0; i < size; i++) { napi_status status = napi_ok; @@ -433,4 +485,4 @@ namespace OHOS::Js_sys_module::Process { } isNeedRun_ = false; } -} // namespace \ No newline at end of file +} // namespace OHOS::Js_sys_module::Process diff --git a/process/js_childprocess.h b/process/js_childprocess.h index b0ebc33..0f56654 100644 --- a/process/js_childprocess.h +++ b/process/js_childprocess.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef BASE_COMPILERUNTIME_JS_SYS_MODULE_CHILD_PROCESS_CLASS_H -#define BASE_COMPILERUNTIME_JS_SYS_MODULE_CHILD_PROCESS_CLASS_H +#ifndef PROCESS_JS_CHILDPROCESS_H_ +#define PROCESS_JS_CHILDPROCESS_H_ #include #include @@ -50,18 +50,78 @@ namespace OHOS::Js_sys_module::Process { class ChildProcess { public: + /** + * Create child process object. + * + * @param env NAPI environment parameters. + */ explicit ChildProcess(napi_env env); + + /** + * Close the target process. + */ void Close(); + + /** + * Send a signal to process. + * + * @param signal Number or string represents the signal sent. + */ void Kill(const napi_value signo); + + /** + * Wait for the child process to finish running, and return a promise object + * whose value is the exit code of the child process. + */ napi_value Wait(); + + /** + * Get the standard output of the child process. + */ napi_value GetOutput() const; + + /** + * Get the standard error output of the child process. + */ napi_value GetErrorOutput() const; + + /** + * Get kill status. + */ napi_value GetKilled() const; + + /** + * Get the specific pid value. + */ napi_value Getpid() const; + + /** + * Get the parent process ID. + */ napi_value Getppid() const; + + /** + * Get exit status. + */ napi_value GetExitCode() const; + + /** + * Initialization option information. + * + * @param options Option parameter. + */ void InitOptionsInfo(napi_value options); + + /** + * Start a subprocess to execute shell commands. + * + * @param command Command parameters. + */ void Spawn(napi_value command); + + /** + * ChildProcess destructor. + */ virtual ~ChildProcess(); private: @@ -88,5 +148,5 @@ namespace OHOS::Js_sys_module::Process { bool killed_ = false; bool isWait_ = true; }; -} // namespace -#endif \ No newline at end of file +} // namespace OHOS::Js_sys_module::Process +#endif // PROCESS_JS_CHILDPROCESS_H_ diff --git a/process/js_process.cpp b/process/js_process.cpp index c89d0f9..c650369 100644 --- a/process/js_process.cpp +++ b/process/js_process.cpp @@ -421,7 +421,7 @@ namespace OHOS::Js_sys_module::Process { return result; } - int Process::ConvertTime(time_t tvsec, long tvnsec) const + int Process::ConvertTime(time_t tvsec, int64_t tvnsec) const { return int(tvsec * 1000) + int(tvnsec / 1000000); // 98999:Only converttime numbers is 1000 and 1000000. } @@ -574,4 +574,4 @@ namespace OHOS::Js_sys_module::Process { } eventMap.clear(); } -} // namespace \ No newline at end of file +} // namespace OHOS::Js_sys_module::Process diff --git a/process/js_process.h b/process/js_process.h index 37c50be..2967f13 100644 --- a/process/js_process.h +++ b/process/js_process.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef BASE_COMPILERUNTIME_JS_SYS_MODULE_PROCESS_CLASS_H -#define BASE_COMPILERUNTIME_JS_SYS_MODULE_PROCESS_CLASS_H +#ifndef PROCESS_JS_PROCESS_H_ +#define PROCESS_JS_PROCESS_H_ #include #include @@ -28,42 +28,184 @@ namespace OHOS::Js_sys_module::Process { enum class PromiseRejectionEvent : uint32_t { REJECT = 0, HANDLE }; class Process { public: + /** + * Create process object + * + * @param env NAPI environment parameters. + */ explicit Process(napi_env env); + + /** + * Process destructor. + */ virtual ~Process() {} + + /** + * Get process uid. + */ napi_value GetUid() const; + + /** + * Get the user ID of the process. + */ napi_value GetGid() const; + + /** + * Get the effective user identity of the process. + */ napi_value GetEUid() const; + + /** + * Get the effective group ID of the process. + */ napi_value GetEGid() const; + + /** + * Get an array with supplementary group ids. + */ napi_value GetGroups() const; + + /** + * Get the pid of the current process. + */ napi_value GetPid() const; + + /** + * Get the pid of the parent process of the current process. + */ napi_value GetPpid() const; + + /** + * Change the current working directory of the process. + * + * @param args The parameter is the path. + */ void Chdir(napi_value args) const; + + /** + * Get the number of seconds the current system has been running. + */ napi_value Uptime() const; + + /** + * Send a signal to the specified process and end the specified process. + * + * @param signal The parameter is the signal sent. + * @param proid The parameter is the id of the process. + */ napi_value Kill(napi_value signal, napi_value proid); + + /** + * Causes the process to exit immediately and generate a core file. + */ void Abort() const; + + /** + * Store user-triggered events. + * + * @param str The parameter is type of storage event. + * @param function The parameter is callback event. + */ void On(napi_value str, napi_value function); + + /** + * Delete user-stored events. + * + * @param str The parameter is the type of delete event. + */ napi_value Off(napi_value str); + + /** + * Terminate the program. + * + * @param number The parameter is the exit code of the process. + */ void Exit(napi_value number) const; + + /** + * Use this method to get the working directory of the process. + */ napi_value Cwd() const; + + /** + * Get the tid of the current process. + */ napi_value GetTid() const; + + /** + * Determines whether the process is isolated. + */ napi_value IsIsolatedProcess() const; + + /** + * Determine whether the uid belongs to the application. + * + * @param uid The parameter is the uid of the application. + */ napi_value IsAppUid(napi_value uid) const; + + /** + * Determine whether the operating environment is 64-bit. + */ napi_value Is64Bit() const; + + /** + * Get process uid by process name. + * + * @param name The parameter is the process name. + */ napi_value GetUidForName(napi_value name) const; + + /** + * Get thread priority based on specified tid. + * + * @param tid The parameter is the specified thread tid. + */ napi_value GetThreadPriority(napi_value tid) const; + + /** + * Get the real-time elapsed time from system startup to process startup. + */ napi_value GetStartRealtime() const; + + /** + * Get the CPU time from the process startup to the current time. + */ napi_value GetPastCputime() const; + + /** + * Get system configuration information. + * + * @param name The parameter is the name of the specified system configuration parameter. + */ napi_value GetSystemConfig(napi_value name) const; + + /** + * Use this method to get the value corresponding to the environment variable. + * + * @param name The parameter is the environment variable name. + */ napi_value GetEnvironmentVar(napi_value name) const; + + /** + * Set reject callback. + */ napi_value SetRejectionCallback() const; + /** + * Clear references to callbacks. + * + * @param env The parameter is NAPI environment variables. + */ static void ClearReference(napi_env env); + private: - int ConvertTime(time_t tvsec, long tvnsec) const; + int ConvertTime(time_t tvsec, int64_t tvnsec) const; + private: napi_env env_ { nullptr }; int FIRST_APPLICATION_UID = 10000; int LAST_APPLICATION_UID = 19999; }; -} -#endif +} // namespace OHOS::Js_sys_module::Process +#endif // PROCESS_JS_PROCESS_H_ \ No newline at end of file diff --git a/process/native_module_process.cpp b/process/native_module_process.cpp index d58144a..ec0b50a 100644 --- a/process/native_module_process.cpp +++ b/process/native_module_process.cpp @@ -94,10 +94,10 @@ namespace OHOS::Js_sys_module::Process { NAPI_CALL(env, napi_wrap( env, thisVar, objectInfo, [](napi_env env, void* data, void* hint) { - auto objectInfo = (ChildProcess*)data; - if (objectInfo != nullptr) { - delete objectInfo; - objectInfo = nullptr; + auto objectResult = reinterpret_cast(data); + if (objectResult != nullptr) { + delete objectResult; + objectResult = nullptr; } }, nullptr, nullptr)); @@ -540,8 +540,10 @@ namespace OHOS::Js_sys_module::Process { NAPI_CALL(env, napi_wrap( env, obj, reinterpret_cast(Process::ClearReference), [](napi_env env, void* data, void* hint) { - ClearRefCallback clearReference = reinterpret_cast(data); - clearReference(env); + if (data != nullptr) { + ClearRefCallback clearParameters = reinterpret_cast(data); + clearParameters(env); + } }, nullptr, nullptr)); NAPI_CALL(env, napi_set_named_property(env, exports, "obj", obj)); @@ -555,7 +557,7 @@ namespace OHOS::Js_sys_module::Process { .nm_filename = nullptr, .nm_register_func = Init, .nm_modname = "process", - .nm_priv = ((void*)0), + .nm_priv = reinterpret_cast(0), .reserved = { 0 }, }; @@ -563,4 +565,4 @@ namespace OHOS::Js_sys_module::Process { { napi_module_register(&processModule); } -} // namespace +} // namespace OHOS::Js_sys_module::Process diff --git a/test/unittest/test.h b/test/unittest/test.h index 466fd55..d03341a 100755 --- a/test/unittest/test.h +++ b/test/unittest/test.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef FOUNDATION_ACE_NAPI_TEST_UNITTEST_TEST_H -#define FOUNDATION_ACE_NAPI_TEST_UNITTEST_TEST_H +#ifndef TEST_UNITTEST_TEST_H_ +#define TEST_UNITTEST_TEST_H_ #include "native_engine.h" @@ -22,12 +22,29 @@ class NativeEngineTest : public testing::Test { public: + /** + * NativeEngineTest constructor. + */ NativeEngineTest(); + + /** + * NativeEngineTest destructor. + */ + virtual ~NativeEngineTest(); + + /** + * Set function. + */ void SetUp() override {} + + /** + * TearDown function. + */ void TearDown() override {} + protected: NativeEngine *engine_; }; -#endif /* FOUNDATION_ACE_NAPI_TEST_UNITTEST_TEST_H */ \ No newline at end of file +#endif // TEST_UNITTEST_TEST_H_ diff --git a/test/unittest/test_process.cpp b/test/unittest/test_process.cpp index b33bf32..83f8405 100755 --- a/test/unittest/test_process.cpp +++ b/test/unittest/test_process.cpp @@ -157,4 +157,4 @@ HWTEST_F(NativeEngineTest, ProcessRunCmdTest002, testing::ext::TestSize.Level0) bool res = false; napi_is_promise(env, errorOutput, &res); ASSERT_TRUE(res); -} \ No newline at end of file +}