1/* 2* Copyright (c) 2021-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/** 17* The process is mainly used to obtain the relevant ID of the process, obtain and modify 18* the working directory of the process, exit and close the process. 19* @since 7 20* @syscap SystemCapability.Utils.Lang 21*/ 22 23declare namespace process { 24 /** 25 * The childprocess object can be used to create a new process. 26 * @since 7 27 * @syscap SystemCapability.Utils.Lang 28 * @systemapi Hide this for inner system use 29 * @test 30 */ 31 export interface ChildProcess { 32 /** 33 * Return pid is the pid of the current process 34 * @since 7 35 * @syscap SystemCapability.Utils.Lang 36 * @systemapi Hide this for inner system use 37 * @test 38 * @returns Return the pid of the current process. 39 */ 40 readonly pid: number; 41 42 /** 43 * Return ppid is the pid of the current child process 44 * @since 7 45 * @syscap SystemCapability.Utils.Lang 46 * @systemapi Hide this for inner system use 47 * @test 48 * @returns Return the pid of the current child process. 49 */ 50 readonly ppid: number; 51 52 /** 53 * Return exitCode is the exit code of the current child process 54 * @since 7 55 * @syscap SystemCapability.Utils.Lang 56 * @systemapi Hide this for inner system use 57 * @test 58 * @returns Return the exit code of the current child process. 59 */ 60 readonly exitCode: number; 61 62 /** 63 * Return boolean is whether the current process signal is sent successfully 64 * @since 7 65 * @syscap SystemCapability.Utils.Lang 66 * @systemapi Hide this for inner system use 67 * @test 68 * @returns Return whether the current process signal is sent successfully. 69 */ 70 readonly killed: boolean; 71 72 /** 73 * Return 'number' is the target process exit code 74 * @since 7 75 * @syscap SystemCapability.Utils.Lang 76 * @systemapi Hide this for inner system use 77 * @test 78 * @returns Return the target process exit code. 79 */ 80 wait(): Promise<number>; 81 82 /** 83 * Return it as 'Uint8Array' of the stdout until EOF 84 * @since 7 85 * @syscap SystemCapability.Utils.Lang 86 * @systemapi Hide this for inner system use 87 * @test 88 * @returns Return subprocess standard output. 89 */ 90 getOutput(): Promise<Uint8Array>; 91 92 /** 93 * Return it as 'Uint8Array of the stderr until EOF 94 * @since 7 95 * @syscap SystemCapability.Utils.Lang 96 * @systemapi Hide this for inner system use 97 * @test 98 * @returns Return subprocess standard error output. 99 */ 100 getErrorOutput(): Promise<Uint8Array>; 101 102 /** 103 * Close the target process 104 * @since 7 105 * @syscap SystemCapability.Utils.Lang 106 * @systemapi Hide this for inner system use 107 * @test 108 */ 109 close(): void; 110 111 /** 112 * Send a signal to process 113 * @since 7 114 * @syscap SystemCapability.Utils.Lang 115 * @systemapi Hide this for inner system use 116 * @test 117 * @param signal Number or string represents the signal sent. 118 */ 119 kill(signal: number | string): void; 120 } 121 122 /** 123 * Process is mainly used to obtain the relevant ID of the process, obtain and modify the 124 * working directory of the process, exit and close the process. 125 * @name ProcessManager 126 * @since 9 127 * @syscap SystemCapability.Utils.Lang 128 */ 129 export class ProcessManager { 130 /** 131 * Returns a boolean whether the specified uid belongs to a particular application. 132 * @since 9 133 * @syscap SystemCapability.Utils.Lang 134 * @param v An id. 135 * @returns Return a boolean whether the specified uid belongs to a particular application. 136 * @throws {BusinessError} 401 - The type of v must be number. 137 */ 138 isAppUid(v: number): boolean; 139 140 /** 141 * Returns the uid based on the specified user name. 142 * @since 9 143 * @syscap SystemCapability.Utils.Lang 144 * @param v Process name. 145 * @returns Return the uid based on the specified user name. 146 * @throws {BusinessError} 401 - The type of v must be string. 147 */ 148 getUidForName(v: string): number; 149 150 /** 151 * Returns the thread priority based on the specified tid. 152 * @since 9 153 * @syscap SystemCapability.Utils.Lang 154 * @param v The tid of the process. 155 * @returns Return the thread priority based on the specified tid. 156 * @throws {BusinessError} 401 - The type of v must be number. 157 */ 158 getThreadPriority(v: number): number; 159 160 /** 161 * Returns the system configuration at runtime. 162 * @since 9 163 * @syscap SystemCapability.Utils.Lang 164 * @param name Parameters defined by the system configuration. 165 * @returns Return the system configuration at runtime. 166 * @throws {BusinessError} 401 - The type of name must be number. 167 */ 168 getSystemConfig(name: number): number; 169 170 /** 171 * Returns the system value for environment variables. 172 * @since 9 173 * @syscap SystemCapability.Utils.Lang 174 * @param name Parameters defined by the system environment variables. 175 * @returns Return the system value for environment variables. 176 * @throws {BusinessError} 401 - The type of name must be string. 177 */ 178 getEnvironmentVar(name: string): string; 179 180 /** 181 * Process exit 182 * @since 9 183 * @syscap SystemCapability.Utils.Lang 184 * @param code Process exit code. 185 * @throws {BusinessError} 401 - The type of code must be number. 186 */ 187 exit(code: number): void; 188 189 /** 190 * Return whether the signal was sent successfully 191 * @since 9 192 * @syscap SystemCapability.Utils.Lang 193 * @param signal Signal sent. 194 * @param pid Send signal to target pid. 195 * @returns Return the result of the signal. 196 * @throws {BusinessError} 401 - if the input parameters are invalid. 197 */ 198 kill(signal: number, pid: number): boolean; 199 } 200 201 /** 202 * Returns the numeric valid group ID of the process 203 * @since 7 204 * @syscap SystemCapability.Utils.Lang 205 * @systemapi Hide this for inner system use 206 * @test 207 * @returns Return the numeric valid group ID of the process. 208 */ 209 const egid: number; 210 211 /** 212 * Return the numeric valid user identity of the process 213 * @since 7 214 * @syscap SystemCapability.Utils.Lang 215 * @systemapi Hide this for inner system use 216 * @test 217 * @returns Return the numeric valid user identity of the process. 218 */ 219 const euid: number; 220 221 /** 222 * Returns the numeric group id of the process 223 * @since 7 224 * @syscap SystemCapability.Utils.Lang 225 * @systemapi Hide this for inner system use 226 * @test 227 * @returns Return the numeric group if of the process. 228 */ 229 const gid: number 230 231 /** 232 * Returns the digital user id of the process 233 * @since 7 234 * @syscap SystemCapability.Utils.Lang 235 * @returns Return the digital user id of the process. 236 */ 237 const uid: number; 238 239 /** 240 * Return an array with supplementary group id 241 * @since 7 242 * @syscap SystemCapability.Utils.Lang 243 * @systemapi Hide this for inner system use 244 * @test 245 * @returns Return an array with supplementary group id. 246 */ 247 const groups: number[]; 248 249 /** 250 * Return pid is The pid of the current process 251 * @since 7 252 * @syscap SystemCapability.Utils.Lang 253 * @returns Return The pid of the current process. 254 */ 255 const pid: number; 256 257 /** 258 * Return ppid is The pid of the current child process 259 * @since 7 260 * @syscap SystemCapability.Utils.Lang 261 * @systemapi Hide this for inner system use 262 * @test 263 * @returns Return The pid of the current child process. 264 */ 265 const ppid: number; 266 267 /** 268 * Returns the tid of the current thread. 269 * @since 8 270 * @syscap SystemCapability.Utils.Lang 271 * @returns Return the tid of the current thread. 272 */ 273 const tid: number; 274 275 /** 276 * Returns a boolean whether the process is isolated. 277 * @since 8 278 * @syscap SystemCapability.Utils.Lang 279 * @returns Return boolean whether the process is isolated. 280 */ 281 function isIsolatedProcess(): boolean; 282 283 /** 284 * Returns a boolean whether the specified uid belongs to a particular application. 285 * @since 8 286 * @deprecated since 9 287 * @useinstead ohos.process.ProcessManager.isAppUid 288 * @syscap SystemCapability.Utils.Lang 289 * @param v An id. 290 * @returns Return a boolean whether the specified uid belongs to a particular application. 291 */ 292 function isAppUid(v: number): boolean; 293 294 /** 295 * Returns a boolean whether the process is running in a 64-bit environment. 296 * @since 8 297 * @syscap SystemCapability.Utils.Lang 298 * @returns Return a boolean whether the process is running in a 64-bit environment. 299 */ 300 function is64Bit(): boolean; 301 302 /** 303 * Returns the uid based on the specified user name. 304 * @since 8 305 * @deprecated since 9 306 * @useinstead ohos.process.ProcessManager.getUidForName 307 * @syscap SystemCapability.Utils.Lang 308 * @param v Process name. 309 * @returns Return the uid based on the specified user name. 310 */ 311 function getUidForName(v: string): number; 312 313 /** 314 * Returns the thread priority based on the specified tid. 315 * @since 8 316 * @deprecated since 9 317 * @useinstead ohos.process.ProcessManager.getThreadPriority 318 * @syscap SystemCapability.Utils.Lang 319 * @param v The tid of the process. 320 * @returns Return the thread priority based on the specified tid. 321 */ 322 function getThreadPriority(v: number): number; 323 324 /** 325 * Returns the elapsed real time (in milliseconds) taken from the start of the system to the start of the process. 326 * @since 8 327 * @syscap SystemCapability.Utils.Lang 328 * @returns Return the start of the system to the start of the process. 329 */ 330 function getStartRealtime(): number; 331 332 /** 333 * Returns the cpu time (in milliseconds) from the time when the process starts to the current time. 334 * @since 8 335 * @syscap SystemCapability.Utils.Lang 336 * @returns Return the cpu time (in milliseconds) from the time when the process starts to the current time. 337 */ 338 function getPastCpuTime(): number; 339 340 /** 341 * Returns the system configuration at runtime. 342 * @since 8 343 * @deprecated since 9 344 * @useinstead ohos.process.ProcessManager.getSystemConfig 345 * @syscap SystemCapability.Utils.Lang 346 * @param name Parameters defined by the system configuration. 347 * @returns Return the system configuration at runtime. 348 */ 349 function getSystemConfig(name: number): number; 350 351 /** 352 * Returns the system value for environment variables. 353 * @since 8 354 * @deprecated since 9 355 * @useinstead ohos.process.ProcessManager.getEnvironmentVar 356 * @syscap SystemCapability.Utils.Lang 357 * @param name Parameters defined by the system environment variables. 358 * @returns Return the system value for environment variables. 359 */ 360 function getEnvironmentVar(name: string): string; 361 362 type EventListener = (evt: Object) => void; 363 /** 364 * Return a child process object and spawns a new ChildProcess to run the command. 365 * @since 7 366 * @syscap SystemCapability.Utils.Lang 367 * @param command String of the shell commands executed by the child process. 368 * @param options This is an object. The object contains three parameters. Timeout is the running time of the child 369 * process, killSignal is the signal sent when the child process reaches timeout, and maxBuffer is the size of the 370 * maximum buffer area for standard input and output. 371 * @systemapi Hide this for inner system use 372 * @test 373 * @returns Return a child process object. 374 */ 375 function runCmd(command: string, 376 options?: { timeout?: number, killSignal?: number | string, maxBuffer?: number }): ChildProcess; 377 378 /** 379 * Abort current process 380 * @since 7 381 * @syscap SystemCapability.Utils.Lang 382 */ 383 function abort(): void; 384 385 /** 386 * Register for an event 387 * @since 7 388 * @syscap SystemCapability.Utils.Lang 389 * @param type Indicates the type of event registered. 390 * @systemapi Hide this for inner system use 391 * @test 392 * @param listener Represents the registered event function 393 */ 394 function on(type: string, listener: EventListener): void; 395 396 /** 397 * Remove registered event 398 * @since 7 399 * @syscap SystemCapability.Utils.Lang 400 * @param type Remove the type of registered event. 401 * @systemapi Hide this for inner system use 402 * @test 403 * @returns Return removed result. 404 */ 405 function off(type: string): boolean; 406 407 /** 408 * Process exit 409 * @since 7 410 * @deprecated since 9 411 * @useinstead ohos.process.ProcessManager.exit 412 * @syscap SystemCapability.Utils.Lang 413 * @param code Process exit code. 414 */ 415 function exit(code: number): void; 416 417 /** 418 * Return the current work directory; 419 * @since 7 420 * @syscap SystemCapability.Utils.Lang 421 * @systemapi Hide this for inner system use 422 * @test 423 * @returns Return the current work directory. 424 */ 425 function cwd(): string; 426 427 /** 428 * Change current directory 429 * @since 7 430 * @syscap SystemCapability.Utils.Lang 431 * @systemapi Hide this for inner system use 432 * @test 433 * @param dir The path you want to change. 434 */ 435 function chdir(dir: string): void; 436 437 /** 438 * Returns the running time of the system 439 * @since 7 440 * @syscap SystemCapability.Utils.Lang 441 * @returns Return the running time of the system. 442 */ 443 function uptime(): number; 444 445 /** 446 * Return whether the signal was sent successfully 447 * @since 7 448 * @deprecated since 9 449 * @useinstead ohos.process.ProcessManager.kill 450 * @syscap SystemCapability.Utils.Lang 451 * @param signal Signal sent. 452 * @param pid Send signal to target pid. 453 * @returns Return the result of the signal. 454 */ 455 function kill(signal: number, pid: number): boolean; 456} 457export default process;