• 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 #ifndef PROCESS_JS_CHILDPROCESS_H
17 #define PROCESS_JS_CHILDPROCESS_H
18 
19 #include <string>
20 #include <sys/types.h>
21 
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 namespace OHOS::JsSysModule::Process {
25     struct StdInfo {
26         napi_async_work worker {nullptr};
27         napi_deferred deferred {nullptr};
28         napi_value promise {nullptr};
29         std::string stdData {};
30         bool *isNeedRun {nullptr};
31         int64_t maxBuffSize {};
32         int fd {};
33         int pid {};
34     };
35 
36     struct OptionsInfo {
37         napi_async_work worker {nullptr};
38         bool *isNeedRun {nullptr};
39         int32_t timeout {};
40         int32_t killSignal {};
41         int64_t maxBuffer {};
42         pid_t pid {};
43     };
44 
45     class ChildProcess {
46     public:
47         /**
48          * Create child process object.
49          */
ChildProcess()50         explicit ChildProcess() {}
51 
52         /**
53          * Close the target process.
54          */
55         void Close();
56 
57         /**
58          * Send a signal to process.
59          *
60          * @param env NAPI environment parameters.
61          * @param signal Number or string represents the signal sent.
62          */
63         void Kill(napi_env env, const napi_value signo);
64 
65         /**
66          * Wait for the child process to finish running, and return a promise object
67          * whose value is the exit code of the child process.
68          *
69          * @param env NAPI environment parameters.
70          */
71         napi_value Wait(napi_env env);
72 
73         /**
74          * Get the standard output of the child process.
75          *
76          * @param env NAPI environment parameters.
77          */
78         napi_value GetOutput(napi_env env) const;
79 
80         /**
81          * Get the standard error output of the child process.
82          *
83          * @param env NAPI environment parameters.
84          */
85         napi_value GetErrorOutput(napi_env env) const;
86 
87         /**
88          * Get kill status.
89          *
90          * @param env NAPI environment parameters.
91          */
92         napi_value GetKilled(napi_env env) const;
93 
94         /**
95          * Get the specific pid value.
96          *
97          * @param env NAPI environment parameters.
98          */
99         napi_value Getpid(napi_env env) const;
100 
101         /**
102          * Get the parent process ID.
103          *
104          * @param env NAPI environment parameters.
105          */
106         napi_value Getppid(napi_env env) const;
107 
108         /**
109          * Get exit status.
110          *
111          * @param env NAPI environment parameters.
112          */
113         napi_value GetExitCode(napi_env env) const;
114 
115         /**
116          * Initialization option information.
117          *
118          * @param env NAPI environment parameters.
119          * @param options Option parameter.
120          */
121         void InitOptionsInfo(napi_env env, napi_value options);
122 
123         /**
124          * Start a subprocess to execute shell commands.
125          *
126          * @param env NAPI environment parameters.
127          * @param command Command parameters.
128          */
129         void Spawn(napi_env env, napi_value command);
130 
131         /**
132          * ChildProcess destructor.
133          */
134         virtual ~ChildProcess();
135 
136     private:
137         static void ReadStdOut(napi_env env, void* data);
138         static void EndStdOut(napi_env env, napi_status status, void* buffer);
139         static void ReadStdErr(napi_env env, void* data);
140         static void EndStdErr(napi_env env, napi_status status, void* buffer);
141         static void TimeoutListener(napi_env env, void* data);
142         std::string RequireStrValue(napi_env env, const napi_value strValue);
143         int GetValidSignal(napi_env env, const napi_value signo);
144         void CreateWorker(napi_env env);
145 
146         OptionsInfo* optionsInfo_ {nullptr};
147         StdInfo* stdOutInfo_ {nullptr};
148         StdInfo* stdErrInfo_ {nullptr};
149 
150         int exitCode_ {};
151         int stdOutFd_[2] {};
152         int stdErrFd_[2] {};
153         int ppid_ {};
154 
155         bool isNeedRun_ {true};
156         bool killed_ {};
157         bool isWait_ {true};
158     };
159 } // namespace OHOS::JsSysModule::Process
160 #endif // PROCESS_JS_CHILDPROCESS_H
161