目录

Process Information Query

Use Cases

Starting from version 6.0.0(20), the app process information query API is provided to obtain information about the started app processes on a device. The process information includes the process ID, command line, parent process PID, user ID, user group ID, process start time, process owner ID type, and process owner ID.

Constraints

  1. Currently, this capability is supported only on PCs/2-in-1 devices.

  2. The maximum number of processes that can be queried at a time is 16.

Service Process

Process description:

  1. Your app calls the query API to obtain the app process information.

  2. Device Security Kit synchronously returns the app process information to your app, which then processes services based on the returned information.

API Description

The following table lists the APIs. For details about the APIs, please refer to API Reference.

API Description
queryAllProcesses(): string Obtains the information about all app processes.
queryProcesses(pids: number[]): string Obtains the information about app processes with the specified PIDs.

Development Procedure

  • When preparing for the development, you need to apply for the ohos.permission.QUERY_AUDIT_EVENT permission.

  • Only enterprise apps in the trustlist can apply for this permission. For details about how to apply for this permission, please refer to Permissions for Enterprise Applications.

  1. Import the Device Security Kit module and related public modules.

    import { securityAudit } from '@kit.DeviceSecurityKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
  2. Obtain the information about a single app process or all app processes.

    • Obtain the information about a single app process.
    const TAG: string = 'SecurityAuditJsTest';
    let pids: number[] = [3622];
    try {
      hilog.info(0x0000, TAG, 'queryProcesses begin.');
      const result = securityAudit.queryProcesses(pids);
      hilog.info(0x0000, TAG, 'Succeeded in queryProcesses.');
      // ...
    } catch (err) {
      let e: BusinessError = err as BusinessError;
      hilog.error(0x0000, TAG, 'queryProcesses failed: %{public}d %{public}s', e.code, e.message);
      // ...
    }
    • Obtains the information about all app processes.
    const TAG: string = 'SecurityAuditJsTest';
    try {
      hilog.info(0x0000, TAG, 'queryAllProcesses begin.');
      const result = securityAudit.queryAllProcesses();
      hilog.info(0x0000, TAG, 'Succeeded in queryAllProcesses.');
      // ...
    } catch (err) {
      let e: BusinessError = err as BusinessError;
      hilog.error(0x0000, TAG, 'queryAllProcesses failed: %{public}d %{public}s', e.code, e.message);
      // ...
    }