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_PROCESS_H
17#define PROCESS_JS_PROCESS_H
18
19#include <cstring>
20#include <map>
21#include <sys/time.h>
22
23#include "napi/native_api.h"
24#include "napi/native_node_api.h"
25
26namespace OHOS::JsSysModule::Process {
27    using ClearRefCallback = void (*)(napi_env env);
28    enum class PromiseRejectionEvent : uint32_t { REJECT = 0, HANDLE };
29    class Process {
30    public:
31        /**
32         * Create process object
33         */
34        explicit Process() {}
35
36        /**
37         * Process destructor.
38         */
39        virtual ~Process() {}
40
41        /**
42         * Get process uid.
43         *
44         * @param env The parameter is NAPI environment variables.
45         */
46        napi_value GetUid(napi_env env) const;
47
48        /**
49         * Get the user ID of the process.
50         *
51         * @param env The parameter is NAPI environment variables.
52         */
53        napi_value GetGid(napi_env env) const;
54
55        /**
56         * Get the effective user identity of the process.
57         *
58         * @param env The parameter is NAPI environment variables.
59         */
60        napi_value GetEUid(napi_env env) const;
61
62        /**
63         * Get the effective group ID of the process.
64         *
65         * @param env The parameter is NAPI environment variables.
66         */
67        napi_value GetEGid(napi_env env) const;
68
69        /**
70         * Get an array with supplementary group ids.
71         *
72         * @param env The parameter is NAPI environment variables.s
73         */
74        napi_value GetGroups(napi_env env) const;
75
76        /**
77         * Get the pid of the current process.
78         *
79         * @param env The parameter is NAPI environment variables.
80         */
81        napi_value GetPid(napi_env env) const;
82
83        /**
84         * Get the pid of the parent process of the current process.
85         *
86         * @param env The parameter is NAPI environment variables.
87         */
88        napi_value GetPpid(napi_env env) const;
89
90        /**
91         * Change the current working directory of the process.
92         *
93         * @param env The parameter is NAPI environment variables.
94         * @param args The parameter is the path.
95         */
96        void Chdir(napi_env env, napi_value args) const;
97
98        /**
99         * Get the number of seconds the current system has been running.
100         *
101         * @param env The parameter is NAPI environment variables.
102         */
103        napi_value Uptime(napi_env env) const;
104
105        /**
106         * Send a signal to the specified process and end the specified process.
107         *
108         * @param env The parameter is NAPI environment variables.
109         * @param signal The parameter is the signal sent.
110         * @param proid The parameter is the id of the process.
111         */
112        napi_value Kill(napi_env env, napi_value signal, napi_value proid);
113
114        /**
115         * Causes the process to exit immediately and generate a core file.
116         */
117        void Abort() const;
118
119        /**
120         * Store user-triggered events.
121         *
122         * @param env The parameter is NAPI environment variables.
123         * @param str The parameter is type of storage event.
124         * @param function The parameter is callback event.
125         */
126        void On(napi_env env, napi_value str, napi_value function);
127
128        /**
129         * Delete user-stored events.
130         *
131         * @param env The parameter is NAPI environment variables.
132         * @param str The parameter is the type of delete event.
133         */
134        napi_value Off(napi_env env, napi_value str);
135
136        /**
137         * Terminate the program.
138         *
139         * @param env The parameter is NAPI environment variables.
140         * @param number The parameter is the exit code of the process.
141         */
142        void Exit(napi_env env, napi_value number) const;
143
144        /**
145         * Use this method to get the working directory of the process.
146         *
147         * @param env The parameter is NAPI environment variables.
148         */
149        napi_value Cwd(napi_env env) const;
150
151        /**
152         * Get the tid of the current process.
153         *
154         * @param env The parameter is NAPI environment variables.
155         */
156        napi_value GetTid(napi_env env) const;
157
158        /**
159         * Determines whether the process is isolated.
160         *
161         * @param env The parameter is NAPI environment variables.
162         */
163        napi_value IsIsolatedProcess(napi_env env) const;
164
165        /**
166         * Determine whether the uid belongs to the application.
167         *
168         * @param env The parameter is NAPI environment variables.
169         * @param uid The parameter is the uid of the application.
170         */
171        napi_value IsAppUid(napi_env env, napi_value uid) const;
172
173        /**
174         * Determine whether the operating environment is 64-bit.
175         *
176         * @param env The parameter is NAPI environment variables.
177         */
178        napi_value Is64Bit(napi_env env) const;
179
180        /**
181         * Get process uid by process name.
182         *
183         * @param env The parameter is NAPI environment variables.
184         * @param name The parameter is the process name.
185         */
186        napi_value GetUidForName(napi_env env, napi_value name) const;
187
188        /**
189         * Get thread priority based on specified tid.
190         *
191         * @param env The parameter is NAPI environment variables.
192         * @param tid The parameter is the specified thread tid.
193         */
194        napi_value GetThreadPriority(napi_env env, napi_value tid) const;
195
196        /**
197         * Get the real-time elapsed time from system startup to process startup.
198         */
199        napi_value GetStartRealtime(napi_env env) const;
200
201        /**
202         * Get the CPU time from the process startup to the current time.
203         *
204         * @param env The parameter is NAPI environment variables.
205         */
206        napi_value GetPastCputime(napi_env env) const;
207
208        /**
209         * Get system configuration information.
210         *
211         * @param env The parameter is NAPI environment variables.
212         * @param name The parameter is the name of the specified system configuration parameter.
213         */
214        napi_value GetSystemConfig(napi_env env, napi_value name) const;
215
216        /**
217         * Use this method to get the value corresponding to the environment variable.
218         *
219         * @param env The parameter is NAPI environment variables.
220         * @param name The parameter is the environment variable name.
221         */
222        napi_value GetEnvironmentVar(napi_env env, napi_value name) const;
223
224        /**
225         * Clear references to callbacks.
226         *
227         * @param env The parameter is NAPI environment variables.
228         */
229        static void ClearReference(napi_env env);
230
231    private:
232        int ConvertTime(time_t tvsec, int64_t tvnsec) const;
233    };
234    class ProcessManager {
235    public:
236        /**
237         * Create process object.
238         */
239        explicit ProcessManager() {}
240
241        /**
242         * Process destructor.
243         */
244        virtual ~ProcessManager() {}
245
246        /**
247         * Determine whether the uid belongs to the application.
248         *
249         * @param env The parameter is NAPI environment variables.
250         * @param uid The parameter is the uid of the application.
251         */
252        napi_value IsAppUid(napi_env env, napi_value uid) const;
253
254        /**
255         * Get process uid by process name.
256         *
257         * @param env The parameter is NAPI environment variables.
258         * @param name The parameter is the process name.
259         */
260        napi_value GetUidForName(napi_env env, napi_value name) const;
261
262        /**
263         * Get thread priority based on specified tid.
264         *
265         * @param env The parameter is NAPI environment variables.
266         * @param tid The parameter is the specified thread tid.
267         */
268        napi_value GetThreadPriority(napi_env env, napi_value tid) const;
269
270        /**
271         * Get system configuration information.
272         *
273         * @param env The parameter is NAPI environment variables.
274         * @param name The parameter is the name of the specified system configuration parameter.
275         */
276        napi_value GetSystemConfig(napi_env env, napi_value name) const;
277
278        /**
279         * Use this method to get the value corresponding to the environment variable.
280         *
281         * @param env The parameter is NAPI environment variables.
282         * @param name The parameter is the environment variable name.
283         */
284        napi_value GetEnvironmentVar(napi_env env, napi_value name) const;
285
286        /**
287         * Terminate the program.
288         *
289         * @param env The parameter is NAPI environment variables.
290         * @param number The parameter is the exit code of the process.
291         */
292        void Exit(napi_env env, napi_value number) const;
293
294        /**
295         * Send a signal to the specified process and end the specified process.
296         *
297         * @param env The parameter is NAPI environment variables.
298         * @param signal The parameter is the signal sent.
299         * @param proid The parameter is the id of the process.
300         */
301        napi_value Kill(napi_env env, napi_value signal, napi_value proid);
302    };
303} // namespace OHOS::JsSysModule::Process
304#endif // PROCESS_JS_PROCESS_H