目录

Process Information Query (C/C++)

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
int32_t HMS_SecurityAudit_QueryAllProcesses(char** result) Obtains the information about all app processes.
int32_t HMS_SecurityAudit_QueryProcesses(uint64_t* pids, uint64_t count, char** result) 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 and link the security audit shared library in CMakeLists.txt.

    find_library(dsm-lib libsecurityaudit_ndk.z.so)
    target_link_libraries(entry PUBLIC libace_napi.z.so ${dsm-lib})
  2. Import the security audit header file.

    #include <cstdio>
    #include "DeviceSecurityKit/security_audit.h"
  3. Obtain the information about a single app process or all app processes.

    After processing services based on the app process information, your app needs to release the memory allocated for the input and output parameters of the query API.

    • Call the HMS_SecurityAudit_QueryProcesses API to obtain the information about a single app process.
    char* result = nullptr;
    uint64_t pids[] = {3266};
    int32_t ret = HMS_SecurityAudit_QueryProcesses(pids, sizeof(pids)/sizeof(pids[0]), &result);
    if (ret == 0 && result != nullptr) {
        printf("HMS_SecurityAudit_QueryProcesses result: %s\n", result);
    } else {
        printf("HMS_SecurityAudit_QueryProcesses failed with error: %d\n", ret);
    }
        
    // ...
    if (result != nullptr) {
        delete[] result;
        result = nullptr;
    }
    • Call the HMS_SecurityAudit_QueryAllProcesses API to obtain the information about all app processes.
    char* result = nullptr;
    int32_t ret = HMS_SecurityAudit_QueryAllProcesses(&result);
    if (ret == 0 && result != nullptr) {
    printf("HMS_SecurityAudit_QueryAllProcesses result: %s\n", result);
    } else {
        printf("HMS_SecurityAudit_QueryAllProcesses failed with error: %d\n", ret);
    }
    // ...
    if (result != nullptr) {
        delete[] result;
        result = nullptr;
    }