169570cc8Sopenharmony_ci/*
269570cc8Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
369570cc8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
469570cc8Sopenharmony_ci * you may not use this file except in compliance with the License.
569570cc8Sopenharmony_ci * You may obtain a copy of the License at
669570cc8Sopenharmony_ci *
769570cc8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
869570cc8Sopenharmony_ci *
969570cc8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1069570cc8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1169570cc8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1269570cc8Sopenharmony_ci * See the License for the specific language governing permissions and
1369570cc8Sopenharmony_ci * limitations under the License.
1469570cc8Sopenharmony_ci */
1569570cc8Sopenharmony_ci
1669570cc8Sopenharmony_ci#include "appspawn_client.h"
1769570cc8Sopenharmony_ci#include "appspawn_mount_permission.h"
1869570cc8Sopenharmony_ci#include "appspawn_hook.h"
1969570cc8Sopenharmony_ci#include "appspawn_utils.h"
2069570cc8Sopenharmony_ci#include "parameter.h"
2169570cc8Sopenharmony_ci#include "securec.h"
2269570cc8Sopenharmony_ci
2369570cc8Sopenharmony_cistatic inline int CalcFlagsUnits(uint32_t maxIndex)
2469570cc8Sopenharmony_ci{
2569570cc8Sopenharmony_ci    return ((maxIndex / 32) + ((maxIndex % 32 == 0) ? 0 : 1));  // 32 max bit in uint32_t
2669570cc8Sopenharmony_ci}
2769570cc8Sopenharmony_ci
2869570cc8Sopenharmony_cistatic inline int SetAppSpawnMsgFlags(AppSpawnMsgFlags *msgFlags, uint32_t index)
2969570cc8Sopenharmony_ci{
3069570cc8Sopenharmony_ci    uint32_t blockIndex = index / 32;  // 32 max bit in int
3169570cc8Sopenharmony_ci    uint32_t bitIndex = index % 32;    // 32 max bit in int
3269570cc8Sopenharmony_ci    if (blockIndex < msgFlags->count) {
3369570cc8Sopenharmony_ci        msgFlags->flags[blockIndex] |= (1 << bitIndex);
3469570cc8Sopenharmony_ci    }
3569570cc8Sopenharmony_ci    return 0;
3669570cc8Sopenharmony_ci}
3769570cc8Sopenharmony_ci
3869570cc8Sopenharmony_cistatic inline int CheckMsg(const AppSpawnReqMsgNode *reqNode, const AppSpawnTlv *tlv, const char *name)
3969570cc8Sopenharmony_ci{
4069570cc8Sopenharmony_ci    if ((reqNode->msg->msgLen + tlv->tlvLen) > MAX_MSG_TOTAL_LENGTH) {
4169570cc8Sopenharmony_ci        APPSPAWN_LOGE("The message is too long %{public}s", name);
4269570cc8Sopenharmony_ci        return APPSPAWN_MSG_INVALID;
4369570cc8Sopenharmony_ci    }
4469570cc8Sopenharmony_ci    if (reqNode->msg->msgType == MSG_GET_RENDER_TERMINATION_STATUS) {
4569570cc8Sopenharmony_ci        if (tlv->tlvType != TLV_RENDER_TERMINATION_INFO) {
4669570cc8Sopenharmony_ci            APPSPAWN_LOGE("Not support tlv %{public}s for message MSG_GET_RENDER_TERMINATION_STATUS", name);
4769570cc8Sopenharmony_ci            return APPSPAWN_TLV_NOT_SUPPORT;
4869570cc8Sopenharmony_ci        }
4969570cc8Sopenharmony_ci    }
5069570cc8Sopenharmony_ci    return 0;
5169570cc8Sopenharmony_ci}
5269570cc8Sopenharmony_ci
5369570cc8Sopenharmony_cistatic inline int CheckInputString(const char *info, const char *value, uint32_t maxLen)
5469570cc8Sopenharmony_ci{
5569570cc8Sopenharmony_ci    APPSPAWN_CHECK(value != NULL, return APPSPAWN_ARG_INVALID, "Invalid input info for %{public}s ", info);
5669570cc8Sopenharmony_ci    uint32_t valueLen = (uint32_t)strlen(value);
5769570cc8Sopenharmony_ci    APPSPAWN_CHECK(valueLen > 0 && valueLen < maxLen, return APPSPAWN_ARG_INVALID,
5869570cc8Sopenharmony_ci        "Invalid input string length '%{public}s' for '%{public}s'", value, info);
5969570cc8Sopenharmony_ci    return 0;
6069570cc8Sopenharmony_ci}
6169570cc8Sopenharmony_ci
6269570cc8Sopenharmony_cistatic AppSpawnMsgBlock *CreateAppSpawnMsgBlock(AppSpawnReqMsgNode *reqNode)
6369570cc8Sopenharmony_ci{
6469570cc8Sopenharmony_ci    AppSpawnMsgBlock *block = (AppSpawnMsgBlock *)calloc(1, MAX_MSG_BLOCK_LEN);
6569570cc8Sopenharmony_ci    APPSPAWN_CHECK(block != NULL, return NULL, "Failed to create block");
6669570cc8Sopenharmony_ci    OH_ListInit(&block->node);
6769570cc8Sopenharmony_ci    block->blockSize = MAX_MSG_BLOCK_LEN - sizeof(AppSpawnMsgBlock);
6869570cc8Sopenharmony_ci    block->currentIndex = 0;
6969570cc8Sopenharmony_ci    OH_ListAddTail(&reqNode->msgBlocks, &block->node);
7069570cc8Sopenharmony_ci    return block;
7169570cc8Sopenharmony_ci}
7269570cc8Sopenharmony_ci
7369570cc8Sopenharmony_cistatic AppSpawnMsgBlock *GetValidMsgBlock(const AppSpawnReqMsgNode *reqNode, uint32_t realLen)
7469570cc8Sopenharmony_ci{
7569570cc8Sopenharmony_ci    AppSpawnMsgBlock *block = NULL;
7669570cc8Sopenharmony_ci    struct ListNode *node = reqNode->msgBlocks.next;
7769570cc8Sopenharmony_ci    while (node != &reqNode->msgBlocks) {
7869570cc8Sopenharmony_ci        block = ListEntry(node, AppSpawnMsgBlock, node);
7969570cc8Sopenharmony_ci        if ((block->blockSize - block->currentIndex) >= realLen) {
8069570cc8Sopenharmony_ci            return block;
8169570cc8Sopenharmony_ci        }
8269570cc8Sopenharmony_ci        node = node->next;
8369570cc8Sopenharmony_ci    }
8469570cc8Sopenharmony_ci    return NULL;
8569570cc8Sopenharmony_ci}
8669570cc8Sopenharmony_ci
8769570cc8Sopenharmony_cistatic AppSpawnMsgBlock *GetTailMsgBlock(const AppSpawnReqMsgNode *reqNode)
8869570cc8Sopenharmony_ci{
8969570cc8Sopenharmony_ci    AppSpawnMsgBlock *block = NULL;
9069570cc8Sopenharmony_ci    struct ListNode *node = reqNode->msgBlocks.prev;
9169570cc8Sopenharmony_ci    if (node != &reqNode->msgBlocks) {
9269570cc8Sopenharmony_ci        block = ListEntry(node, AppSpawnMsgBlock, node);
9369570cc8Sopenharmony_ci    }
9469570cc8Sopenharmony_ci    return block;
9569570cc8Sopenharmony_ci}
9669570cc8Sopenharmony_ci
9769570cc8Sopenharmony_cistatic void FreeMsgBlock(ListNode *node)
9869570cc8Sopenharmony_ci{
9969570cc8Sopenharmony_ci    AppSpawnMsgBlock *block = ListEntry(node, AppSpawnMsgBlock, node);
10069570cc8Sopenharmony_ci    OH_ListRemove(node);
10169570cc8Sopenharmony_ci    OH_ListInit(node);
10269570cc8Sopenharmony_ci    free(block);
10369570cc8Sopenharmony_ci}
10469570cc8Sopenharmony_ci
10569570cc8Sopenharmony_cistatic int AddAppDataToBlock(AppSpawnMsgBlock *block, const uint8_t *data, uint32_t dataLen, int32_t dataType)
10669570cc8Sopenharmony_ci{
10769570cc8Sopenharmony_ci    APPSPAWN_CHECK(block->blockSize > block->currentIndex,
10869570cc8Sopenharmony_ci        return APPSPAWN_BUFFER_NOT_ENOUGH, "Not enough buffer for data");
10969570cc8Sopenharmony_ci    uint32_t reminderLen = block->blockSize - block->currentIndex;
11069570cc8Sopenharmony_ci    uint32_t realDataLen = (dataType == DATA_TYPE_STRING) ? APPSPAWN_ALIGN(dataLen + 1) : APPSPAWN_ALIGN(dataLen);
11169570cc8Sopenharmony_ci    APPSPAWN_CHECK(reminderLen >= realDataLen, return APPSPAWN_BUFFER_NOT_ENOUGH, "Not enough buffer for data");
11269570cc8Sopenharmony_ci    int ret = memcpy_s(block->buffer + block->currentIndex, reminderLen, data, dataLen);
11369570cc8Sopenharmony_ci    APPSPAWN_CHECK(ret == EOK, return APPSPAWN_SYSTEM_ERROR, "Failed to copy data");
11469570cc8Sopenharmony_ci    if (dataType == DATA_TYPE_STRING) {
11569570cc8Sopenharmony_ci        *((char *)block->buffer + block->currentIndex + dataLen) = '\0';
11669570cc8Sopenharmony_ci    }
11769570cc8Sopenharmony_ci    block->currentIndex += realDataLen;
11869570cc8Sopenharmony_ci    return 0;
11969570cc8Sopenharmony_ci}
12069570cc8Sopenharmony_ci
12169570cc8Sopenharmony_cistatic int AddAppDataToTail(AppSpawnReqMsgNode *reqNode, const uint8_t *data, uint32_t dataLen, int32_t dataType)
12269570cc8Sopenharmony_ci{
12369570cc8Sopenharmony_ci    uint32_t currLen = 0;
12469570cc8Sopenharmony_ci    AppSpawnMsgBlock *block = GetTailMsgBlock(reqNode);
12569570cc8Sopenharmony_ci    APPSPAWN_CHECK(block != NULL, return APPSPAWN_BUFFER_NOT_ENOUGH, "Not block info reqNode");
12669570cc8Sopenharmony_ci    uint32_t realDataLen = (dataType == DATA_TYPE_STRING) ? dataLen + 1 : dataLen;
12769570cc8Sopenharmony_ci    do {
12869570cc8Sopenharmony_ci        uint32_t reminderBufferLen = block->blockSize - block->currentIndex;
12969570cc8Sopenharmony_ci        uint32_t reminderDataLen = realDataLen - currLen;
13069570cc8Sopenharmony_ci        uint32_t realLen = APPSPAWN_ALIGN(reminderDataLen);
13169570cc8Sopenharmony_ci        uint32_t realCopy = 0;
13269570cc8Sopenharmony_ci        if (reminderBufferLen >= realLen) {  // 足够存储,直接保存
13369570cc8Sopenharmony_ci            int ret = memcpy_s(block->buffer + block->currentIndex, reminderBufferLen, data + currLen, reminderDataLen);
13469570cc8Sopenharmony_ci            APPSPAWN_CHECK(ret == EOK, return APPSPAWN_SYSTEM_ERROR, "Failed to copy data");
13569570cc8Sopenharmony_ci            block->currentIndex += realLen;
13669570cc8Sopenharmony_ci            break;
13769570cc8Sopenharmony_ci        } else if (reminderBufferLen > 0) {
13869570cc8Sopenharmony_ci            realCopy = reminderDataLen > reminderBufferLen ? reminderBufferLen : reminderDataLen;
13969570cc8Sopenharmony_ci            int ret = memcpy_s(block->buffer + block->currentIndex, reminderBufferLen, data + currLen, realCopy);
14069570cc8Sopenharmony_ci            APPSPAWN_CHECK(ret == EOK, return APPSPAWN_SYSTEM_ERROR, "Failed to copy data");
14169570cc8Sopenharmony_ci            block->currentIndex += realCopy;
14269570cc8Sopenharmony_ci            currLen += realCopy;
14369570cc8Sopenharmony_ci        }
14469570cc8Sopenharmony_ci        block = CreateAppSpawnMsgBlock(reqNode);
14569570cc8Sopenharmony_ci        APPSPAWN_CHECK(block != NULL, return APPSPAWN_SYSTEM_ERROR, "Not enough buffer for data");
14669570cc8Sopenharmony_ci    } while (currLen < realDataLen);
14769570cc8Sopenharmony_ci    return 0;
14869570cc8Sopenharmony_ci}
14969570cc8Sopenharmony_ci
15069570cc8Sopenharmony_cistatic int AddAppDataEx(AppSpawnReqMsgNode *reqNode, const char *name, const AppSpawnAppData *data)
15169570cc8Sopenharmony_ci{
15269570cc8Sopenharmony_ci    AppSpawnTlvExt tlv = {};
15369570cc8Sopenharmony_ci    if (data->dataType == DATA_TYPE_STRING) {
15469570cc8Sopenharmony_ci        tlv.tlvLen = APPSPAWN_ALIGN(data->dataLen + 1) + sizeof(AppSpawnTlvExt);
15569570cc8Sopenharmony_ci    } else {
15669570cc8Sopenharmony_ci        tlv.tlvLen = APPSPAWN_ALIGN(data->dataLen) + sizeof(AppSpawnTlvExt);
15769570cc8Sopenharmony_ci    }
15869570cc8Sopenharmony_ci    tlv.tlvType = TLV_MAX;
15969570cc8Sopenharmony_ci    tlv.dataLen = data->dataLen;
16069570cc8Sopenharmony_ci    tlv.dataType = data->dataType;
16169570cc8Sopenharmony_ci    int ret = strcpy_s(tlv.tlvName, sizeof(tlv.tlvName), name);
16269570cc8Sopenharmony_ci    APPSPAWN_CHECK(ret == 0, return APPSPAWN_SYSTEM_ERROR, "Failed to add data for %{public}s", name);
16369570cc8Sopenharmony_ci    ret = CheckMsg(reqNode, (AppSpawnTlv *)&tlv, name);
16469570cc8Sopenharmony_ci    APPSPAWN_CHECK_ONLY_EXPER(ret == 0, return ret);
16569570cc8Sopenharmony_ci
16669570cc8Sopenharmony_ci    APPSPAWN_LOGV("AddAppDataEx tlv [%{public}s %{public}u ] dataLen: %{public}u start: %{public}u",
16769570cc8Sopenharmony_ci        name, tlv.tlvLen, data->dataLen, reqNode->msg->msgLen);
16869570cc8Sopenharmony_ci    // 获取一个能保存改完整tlv的block
16969570cc8Sopenharmony_ci    AppSpawnMsgBlock *block = GetValidMsgBlock(reqNode, tlv.tlvLen);
17069570cc8Sopenharmony_ci    if (block != NULL) {
17169570cc8Sopenharmony_ci        ret = AddAppDataToBlock(block, (uint8_t *)&tlv, sizeof(tlv), 0);
17269570cc8Sopenharmony_ci        APPSPAWN_CHECK(ret == 0, return ret, "Failed to add tlv for %{public}s", name);
17369570cc8Sopenharmony_ci        ret = AddAppDataToBlock(block, data->data, data->dataLen, data->dataType);
17469570cc8Sopenharmony_ci        APPSPAWN_CHECK(ret == 0, return ret, "Failed to add data for %{public}s", name);
17569570cc8Sopenharmony_ci    } else {
17669570cc8Sopenharmony_ci        // 没有一个可用的block,最队列最后添加数据
17769570cc8Sopenharmony_ci        ret = AddAppDataToTail(reqNode, (uint8_t *)&tlv, sizeof(tlv), 0);
17869570cc8Sopenharmony_ci        APPSPAWN_CHECK(ret == 0, return ret, "Failed to add tlv to tail for %{public}s", name);
17969570cc8Sopenharmony_ci        ret = AddAppDataToTail(reqNode, data->data, data->dataLen, data->dataType);
18069570cc8Sopenharmony_ci        APPSPAWN_CHECK(ret == 0, return ret, "Failed to add data to tail for %{public}s", name);
18169570cc8Sopenharmony_ci    }
18269570cc8Sopenharmony_ci    reqNode->msg->tlvCount++;
18369570cc8Sopenharmony_ci    reqNode->msg->msgLen += tlv.tlvLen;
18469570cc8Sopenharmony_ci    APPSPAWN_LOGV("AddAppDataEx success name '%{public}s' end: %{public}u", name, reqNode->msg->msgLen);
18569570cc8Sopenharmony_ci    return 0;
18669570cc8Sopenharmony_ci}
18769570cc8Sopenharmony_ci
18869570cc8Sopenharmony_cistatic int AddAppData(AppSpawnReqMsgNode *reqNode,
18969570cc8Sopenharmony_ci    uint32_t tlvType, const AppSpawnAppData *data, uint32_t count, const char *name)
19069570cc8Sopenharmony_ci{
19169570cc8Sopenharmony_ci    // 计算实际数据的长度
19269570cc8Sopenharmony_ci    uint32_t realLen = sizeof(AppSpawnTlv);
19369570cc8Sopenharmony_ci    uint32_t dataLen = 0;
19469570cc8Sopenharmony_ci    for (uint32_t index = 0; index < count; index++) {
19569570cc8Sopenharmony_ci        dataLen += data[index].dataLen;
19669570cc8Sopenharmony_ci        realLen += (data[index].dataType == DATA_TYPE_STRING) ?
19769570cc8Sopenharmony_ci            APPSPAWN_ALIGN(data[index].dataLen + 1) : APPSPAWN_ALIGN(data[index].dataLen);
19869570cc8Sopenharmony_ci    }
19969570cc8Sopenharmony_ci    AppSpawnTlv tlv;
20069570cc8Sopenharmony_ci    tlv.tlvLen = realLen;
20169570cc8Sopenharmony_ci    tlv.tlvType = tlvType;
20269570cc8Sopenharmony_ci    int ret = CheckMsg(reqNode, &tlv, name);
20369570cc8Sopenharmony_ci    APPSPAWN_CHECK_ONLY_EXPER(ret == 0, return ret);
20469570cc8Sopenharmony_ci
20569570cc8Sopenharmony_ci    APPSPAWN_LOGV("AddAppData tlv [%{public}s %{public}u] dataLen: %{public}u start: %{public}u",
20669570cc8Sopenharmony_ci        name, tlv.tlvLen, dataLen, reqNode->msg->msgLen);
20769570cc8Sopenharmony_ci    // 获取一个能保存改完整tlv的block
20869570cc8Sopenharmony_ci    AppSpawnMsgBlock *block = GetValidMsgBlock(reqNode, tlv.tlvLen);
20969570cc8Sopenharmony_ci    if (block != NULL) {
21069570cc8Sopenharmony_ci        ret = AddAppDataToBlock(block, (uint8_t *)&tlv, sizeof(tlv), 0);
21169570cc8Sopenharmony_ci        APPSPAWN_CHECK(ret == 0, return ret, "Failed to add tlv for %{public}d", tlvType);
21269570cc8Sopenharmony_ci
21369570cc8Sopenharmony_ci        for (uint32_t index = 0; index < count; index++) {
21469570cc8Sopenharmony_ci            ret = AddAppDataToBlock(block, (uint8_t *)data[index].data, data[index].dataLen, data[index].dataType);
21569570cc8Sopenharmony_ci            APPSPAWN_CHECK(ret == 0, return ret, "Failed to add data for %{public}d", tlvType);
21669570cc8Sopenharmony_ci        }
21769570cc8Sopenharmony_ci    } else {
21869570cc8Sopenharmony_ci        // 没有一个可用的block,最队列最后添加数据
21969570cc8Sopenharmony_ci        ret = AddAppDataToTail(reqNode, (uint8_t *)&tlv, sizeof(tlv), 0);
22069570cc8Sopenharmony_ci        APPSPAWN_CHECK(ret == 0, return ret, "Failed to add tlv to tail for %{public}d", tlvType);
22169570cc8Sopenharmony_ci        // 添加tlv信息
22269570cc8Sopenharmony_ci        for (uint32_t index = 0; index < count; index++) {
22369570cc8Sopenharmony_ci            ret = AddAppDataToTail(reqNode, (uint8_t *)data[index].data, data[index].dataLen, data[index].dataType);
22469570cc8Sopenharmony_ci            APPSPAWN_CHECK(ret == 0, return ret, "Failed to add data for %{public}d", tlvType);
22569570cc8Sopenharmony_ci        }
22669570cc8Sopenharmony_ci    }
22769570cc8Sopenharmony_ci    reqNode->msg->msgLen += tlv.tlvLen;
22869570cc8Sopenharmony_ci    APPSPAWN_LOGV("AddAppData success tlvType %{public}s end: %{public}u", name, reqNode->msg->msgLen);
22969570cc8Sopenharmony_ci    return 0;
23069570cc8Sopenharmony_ci}
23169570cc8Sopenharmony_ci
23269570cc8Sopenharmony_cistatic int SetFlagsTlv(AppSpawnReqMsgNode *reqNode,
23369570cc8Sopenharmony_ci    AppSpawnMsgBlock *block, AppSpawnMsgFlags **msgFlags, int type, int maxCount)
23469570cc8Sopenharmony_ci{
23569570cc8Sopenharmony_ci    uint32_t units = (uint32_t)CalcFlagsUnits(maxCount);
23669570cc8Sopenharmony_ci    APPSPAWN_LOGV("SetFlagsTlv maxCount %{public}d type %{public}d units %{public}d", maxCount, type, units);
23769570cc8Sopenharmony_ci    uint32_t flagsLen = sizeof(AppSpawnTlv) + sizeof(AppSpawnMsgFlags) + sizeof(uint32_t) * units;
23869570cc8Sopenharmony_ci    APPSPAWN_CHECK((block->blockSize - block->currentIndex) > flagsLen,
23969570cc8Sopenharmony_ci        return APPSPAWN_BUFFER_NOT_ENOUGH, "Invalid block to set flags tlv type %{public}d", type);
24069570cc8Sopenharmony_ci
24169570cc8Sopenharmony_ci    AppSpawnTlv *tlv = (AppSpawnTlv *)(block->buffer + block->currentIndex);
24269570cc8Sopenharmony_ci    tlv->tlvLen = flagsLen;
24369570cc8Sopenharmony_ci    tlv->tlvType = type;
24469570cc8Sopenharmony_ci    *msgFlags = (AppSpawnMsgFlags *)(block->buffer + block->currentIndex + sizeof(AppSpawnTlv));
24569570cc8Sopenharmony_ci    (*msgFlags)->count = units;
24669570cc8Sopenharmony_ci    block->currentIndex += flagsLen;
24769570cc8Sopenharmony_ci    reqNode->msg->msgLen += flagsLen;
24869570cc8Sopenharmony_ci    reqNode->msg->tlvCount++;
24969570cc8Sopenharmony_ci    return 0;
25069570cc8Sopenharmony_ci}
25169570cc8Sopenharmony_ci
25269570cc8Sopenharmony_cistatic int CreateBaseMsg(AppSpawnReqMsgNode *reqNode, uint32_t msgType, const char *processName)
25369570cc8Sopenharmony_ci{
25469570cc8Sopenharmony_ci    AppSpawnMsgBlock *block = CreateAppSpawnMsgBlock(reqNode);
25569570cc8Sopenharmony_ci    APPSPAWN_CHECK(block != NULL, return APPSPAWN_SYSTEM_ERROR, "Failed to create block for %{public}s", processName);
25669570cc8Sopenharmony_ci
25769570cc8Sopenharmony_ci    // 保留消息头的大小
25869570cc8Sopenharmony_ci    reqNode->msg = (AppSpawnMsg *)(block->buffer + block->currentIndex);
25969570cc8Sopenharmony_ci    reqNode->msg->magic = APPSPAWN_MSG_MAGIC;
26069570cc8Sopenharmony_ci    reqNode->msg->msgId = 0;
26169570cc8Sopenharmony_ci    reqNode->msg->msgType = msgType;
26269570cc8Sopenharmony_ci    reqNode->msg->msgLen = sizeof(AppSpawnMsg);
26369570cc8Sopenharmony_ci    reqNode->msg->tlvCount = 0;
26469570cc8Sopenharmony_ci    int ret = strcpy_s(reqNode->msg->processName, sizeof(reqNode->msg->processName), processName);
26569570cc8Sopenharmony_ci    APPSPAWN_CHECK(ret == 0, return APPSPAWN_SYSTEM_ERROR, "Failed to create block for %{public}s", processName);
26669570cc8Sopenharmony_ci    block->currentIndex = sizeof(AppSpawnMsg);
26769570cc8Sopenharmony_ci    ret = SetFlagsTlv(reqNode, block, &reqNode->msgFlags, TLV_MSG_FLAGS, MAX_FLAGS_INDEX);
26869570cc8Sopenharmony_ci    APPSPAWN_CHECK_ONLY_EXPER(ret == 0, return ret);
26969570cc8Sopenharmony_ci    APPSPAWN_CHECK_ONLY_EXPER(msgType == MSG_APP_SPAWN || msgType == MSG_SPAWN_NATIVE_PROCESS, return 0);
27069570cc8Sopenharmony_ci    int maxCount = GetPermissionMaxCount();
27169570cc8Sopenharmony_ci    APPSPAWN_CHECK(maxCount > 0, return APPSPAWN_SYSTEM_ERROR, "Invalid max for permission %{public}s", processName);
27269570cc8Sopenharmony_ci    ret = SetFlagsTlv(reqNode, block, &reqNode->permissionFlags, TLV_PERMISSION, maxCount);
27369570cc8Sopenharmony_ci    APPSPAWN_CHECK_ONLY_EXPER(ret == 0, return ret);
27469570cc8Sopenharmony_ci    APPSPAWN_LOGV("CreateBaseMsg msgLen: %{public}u %{public}u", reqNode->msg->msgLen, block->currentIndex);
27569570cc8Sopenharmony_ci    return 0;
27669570cc8Sopenharmony_ci}
27769570cc8Sopenharmony_ci
27869570cc8Sopenharmony_cistatic void DeleteAppSpawnReqMsg(AppSpawnReqMsgNode *reqNode)
27969570cc8Sopenharmony_ci{
28069570cc8Sopenharmony_ci    APPSPAWN_CHECK_ONLY_EXPER(reqNode != NULL, return);
28169570cc8Sopenharmony_ci    APPSPAWN_LOGV("DeleteAppSpawnReqMsg reqId: %{public}u", reqNode->reqId);
28269570cc8Sopenharmony_ci    reqNode->msgFlags = NULL;
28369570cc8Sopenharmony_ci    reqNode->permissionFlags = NULL;
28469570cc8Sopenharmony_ci    reqNode->msg = NULL;
28569570cc8Sopenharmony_ci    // 释放block
28669570cc8Sopenharmony_ci    OH_ListRemoveAll(&reqNode->msgBlocks, FreeMsgBlock);
28769570cc8Sopenharmony_ci    free(reqNode);
28869570cc8Sopenharmony_ci}
28969570cc8Sopenharmony_ci
29069570cc8Sopenharmony_cistatic AppSpawnReqMsgNode *CreateAppSpawnReqMsg(uint32_t msgType, const char *processName)
29169570cc8Sopenharmony_ci{
29269570cc8Sopenharmony_ci    static uint32_t reqId = 0;
29369570cc8Sopenharmony_ci    AppSpawnReqMsgNode *reqNode = (AppSpawnReqMsgNode *)malloc(sizeof(AppSpawnReqMsgNode));
29469570cc8Sopenharmony_ci    APPSPAWN_CHECK(reqNode != NULL, return NULL, "Failed to create msg node for %{public}s", processName);
29569570cc8Sopenharmony_ci
29669570cc8Sopenharmony_ci    OH_ListInit(&reqNode->node);
29769570cc8Sopenharmony_ci    OH_ListInit(&reqNode->msgBlocks);
29869570cc8Sopenharmony_ci    reqNode->reqId = ++reqId;
29969570cc8Sopenharmony_ci    reqNode->msg = NULL;
30069570cc8Sopenharmony_ci    reqNode->msgFlags = NULL;
30169570cc8Sopenharmony_ci    reqNode->permissionFlags = NULL;
30269570cc8Sopenharmony_ci    reqNode->fdCount = 0;
30369570cc8Sopenharmony_ci    reqNode->isAsan = 0;
30469570cc8Sopenharmony_ci    int ret = CreateBaseMsg(reqNode, msgType, processName);
30569570cc8Sopenharmony_ci    APPSPAWN_CHECK(ret == 0, DeleteAppSpawnReqMsg(reqNode);
30669570cc8Sopenharmony_ci         return NULL, "Failed to create base msg for %{public}s", processName);
30769570cc8Sopenharmony_ci    APPSPAWN_LOGV("CreateAppSpawnReqMsg reqId: %{public}d msg type: %{public}u processName: %{public}s",
30869570cc8Sopenharmony_ci        reqNode->reqId, msgType, processName);
30969570cc8Sopenharmony_ci    return reqNode;
31069570cc8Sopenharmony_ci}
31169570cc8Sopenharmony_ci
31269570cc8Sopenharmony_ciint AppSpawnReqMsgAddFd(AppSpawnReqMsgHandle reqHandle, const char* fdName, int fd)
31369570cc8Sopenharmony_ci{
31469570cc8Sopenharmony_ci    APPSPAWN_CHECK(reqHandle != NULL, return APPSPAWN_ARG_INVALID, "Invalid reqHandle");
31569570cc8Sopenharmony_ci    AppSpawnReqMsgNode *reqNode = (AppSpawnReqMsgNode *)reqHandle;
31669570cc8Sopenharmony_ci    APPSPAWN_CHECK(reqNode != NULL, return APPSPAWN_ARG_INVALID, "Invalid reqNode");
31769570cc8Sopenharmony_ci    APPSPAWN_CHECK(fd >= 0 && fdName != NULL && strlen(fdName) <= APP_FDNAME_MAXLEN
31869570cc8Sopenharmony_ci        &&  reqNode->fdCount < APP_MAX_FD_COUNT, return APPSPAWN_ARG_INVALID,
31969570cc8Sopenharmony_ci        "Invalid fdinfo %{public}d %{public}d %{public}d", fd, fdName != NULL, reqNode->fdCount);
32069570cc8Sopenharmony_ci    reqNode->fds[reqNode->fdCount++] = fd;
32169570cc8Sopenharmony_ci    return AppSpawnReqMsgAddStringInfo(reqHandle, MSG_EXT_NAME_APP_FD, (void *)fdName);
32269570cc8Sopenharmony_ci}
32369570cc8Sopenharmony_ci
32469570cc8Sopenharmony_cistatic void GetSpecialGid(const char *bundleName, gid_t gidTable[], uint32_t *gidCount)
32569570cc8Sopenharmony_ci{
32669570cc8Sopenharmony_ci    // special handle bundle name medialibrary and scanner
32769570cc8Sopenharmony_ci    const char *specialBundleNames[] = {
32869570cc8Sopenharmony_ci        "com.ohos.medialibrary.medialibrarydata", "com.ohos.medialibrary.medialibrarydata:backup"
32969570cc8Sopenharmony_ci    };
33069570cc8Sopenharmony_ci
33169570cc8Sopenharmony_ci    for (size_t i = 0; i < sizeof(specialBundleNames) / sizeof(specialBundleNames[0]); i++) {
33269570cc8Sopenharmony_ci        if (strcmp(bundleName, specialBundleNames[i]) == 0) {
33369570cc8Sopenharmony_ci            if (*gidCount < APP_MAX_GIDS) {
33469570cc8Sopenharmony_ci                gidTable[(*gidCount)++] = GID_USER_DATA_RW;
33569570cc8Sopenharmony_ci                gidTable[(*gidCount)++] = GID_FILE_ACCESS;
33669570cc8Sopenharmony_ci            }
33769570cc8Sopenharmony_ci            break;
33869570cc8Sopenharmony_ci        }
33969570cc8Sopenharmony_ci    }
34069570cc8Sopenharmony_ci}
34169570cc8Sopenharmony_ci
34269570cc8Sopenharmony_ciint AppSpawnReqMsgCreate(AppSpawnMsgType msgType, const char *processName, AppSpawnReqMsgHandle *reqHandle)
34369570cc8Sopenharmony_ci{
34469570cc8Sopenharmony_ci    APPSPAWN_CHECK(reqHandle != NULL, return APPSPAWN_ARG_INVALID, "Invalid request handle");
34569570cc8Sopenharmony_ci    APPSPAWN_CHECK(msgType < MAX_TYPE_INVALID,
34669570cc8Sopenharmony_ci        return APPSPAWN_MSG_INVALID, "Invalid message type %{public}u %{public}s", msgType, processName);
34769570cc8Sopenharmony_ci    int ret = CheckInputString("processName", processName, APP_LEN_PROC_NAME);
34869570cc8Sopenharmony_ci    APPSPAWN_CHECK_ONLY_EXPER(ret == 0, return ret);
34969570cc8Sopenharmony_ci    AppSpawnReqMsgNode *reqNode = CreateAppSpawnReqMsg(msgType, processName);
35069570cc8Sopenharmony_ci    APPSPAWN_CHECK(reqNode != NULL, return APPSPAWN_SYSTEM_ERROR,
35169570cc8Sopenharmony_ci        "Failed to create msg node for %{public}s", processName);
35269570cc8Sopenharmony_ci    *reqHandle = (AppSpawnReqMsgHandle)(reqNode);
35369570cc8Sopenharmony_ci    return 0;
35469570cc8Sopenharmony_ci}
35569570cc8Sopenharmony_ci
35669570cc8Sopenharmony_civoid AppSpawnReqMsgFree(AppSpawnReqMsgHandle reqHandle)
35769570cc8Sopenharmony_ci{
35869570cc8Sopenharmony_ci    AppSpawnReqMsgNode *reqNode = (AppSpawnReqMsgNode *)reqHandle;
35969570cc8Sopenharmony_ci    APPSPAWN_CHECK_ONLY_EXPER(reqNode != NULL, return);
36069570cc8Sopenharmony_ci    DeleteAppSpawnReqMsg(reqNode);
36169570cc8Sopenharmony_ci}
36269570cc8Sopenharmony_ci
36369570cc8Sopenharmony_ciint AppSpawnReqMsgSetAppDacInfo(AppSpawnReqMsgHandle reqHandle, const AppDacInfo *dacInfo)
36469570cc8Sopenharmony_ci{
36569570cc8Sopenharmony_ci    AppSpawnReqMsgNode *reqNode = (AppSpawnReqMsgNode *)reqHandle;
36669570cc8Sopenharmony_ci    APPSPAWN_CHECK_ONLY_EXPER(reqNode != NULL, return APPSPAWN_ARG_INVALID);
36769570cc8Sopenharmony_ci    APPSPAWN_CHECK(dacInfo != NULL, return APPSPAWN_ARG_INVALID, "Invalid dacInfo ");
36869570cc8Sopenharmony_ci
36969570cc8Sopenharmony_ci    AppDacInfo tmpDacInfo = {0};
37069570cc8Sopenharmony_ci    (void)memcpy_s(&tmpDacInfo, sizeof(tmpDacInfo), dacInfo, sizeof(tmpDacInfo));
37169570cc8Sopenharmony_ci    GetSpecialGid(reqNode->msg->processName, tmpDacInfo.gidTable, &tmpDacInfo.gidCount);
37269570cc8Sopenharmony_ci
37369570cc8Sopenharmony_ci    AppSpawnAppData data[MAX_DATA_IN_TLV] = {};
37469570cc8Sopenharmony_ci    data[0].data = (uint8_t *)&tmpDacInfo;
37569570cc8Sopenharmony_ci    data[0].dataLen = sizeof(AppSpawnMsgDacInfo);
37669570cc8Sopenharmony_ci    return AddAppData(reqNode, TLV_DAC_INFO, data, 1, "TLV_DAC_INFO");
37769570cc8Sopenharmony_ci}
37869570cc8Sopenharmony_ci
37969570cc8Sopenharmony_ciint AppSpawnReqMsgSetBundleInfo(AppSpawnReqMsgHandle reqHandle, uint32_t bundleIndex, const char *bundleName)
38069570cc8Sopenharmony_ci{
38169570cc8Sopenharmony_ci    AppSpawnReqMsgNode *reqNode = (AppSpawnReqMsgNode *)reqHandle;
38269570cc8Sopenharmony_ci    APPSPAWN_CHECK_ONLY_EXPER(reqNode != NULL, return APPSPAWN_ARG_INVALID);
38369570cc8Sopenharmony_ci    int ret = CheckInputString("TLV_BUNDLE_INFO", bundleName, APP_LEN_BUNDLE_NAME);
38469570cc8Sopenharmony_ci    APPSPAWN_CHECK_ONLY_EXPER(ret == 0, return ret);
38569570cc8Sopenharmony_ci
38669570cc8Sopenharmony_ci    AppSpawnMsgBundleInfo info = {};
38769570cc8Sopenharmony_ci    info.bundleIndex = bundleIndex;
38869570cc8Sopenharmony_ci    AppSpawnAppData data[MAX_DATA_IN_TLV] = {};
38969570cc8Sopenharmony_ci    data[0].data = (uint8_t *)&info;
39069570cc8Sopenharmony_ci    data[0].dataLen = sizeof(AppSpawnMsgBundleInfo);
39169570cc8Sopenharmony_ci    data[1].data = (uint8_t *)bundleName;
39269570cc8Sopenharmony_ci    data[1].dataLen = strlen(bundleName);
39369570cc8Sopenharmony_ci    data[1].dataType = DATA_TYPE_STRING;
39469570cc8Sopenharmony_ci    return AddAppData(reqNode, TLV_BUNDLE_INFO, data, MAX_DATA_IN_TLV, "TLV_BUNDLE_INFO");
39569570cc8Sopenharmony_ci}
39669570cc8Sopenharmony_ci
39769570cc8Sopenharmony_cistatic int CheckEnabled(const char *param, const char *value)
39869570cc8Sopenharmony_ci{
39969570cc8Sopenharmony_ci    char tmp[32] = {0}; // 32 max
40069570cc8Sopenharmony_ci    int ret = GetParameter(param, "", tmp, sizeof(tmp));
40169570cc8Sopenharmony_ci    APPSPAWN_LOGV("CheckEnabled key %{public}s ret %{public}d result: %{public}s", param, ret, tmp);
40269570cc8Sopenharmony_ci    int enabled = (ret > 0 && strcmp(tmp, value) == 0);
40369570cc8Sopenharmony_ci    return enabled;
40469570cc8Sopenharmony_ci}
40569570cc8Sopenharmony_ci
40669570cc8Sopenharmony_ciint AppSpawnReqMsgSetAppFlag(AppSpawnReqMsgHandle reqHandle, AppFlagsIndex flagIndex)
40769570cc8Sopenharmony_ci{
40869570cc8Sopenharmony_ci    AppSpawnReqMsgNode *reqNode = (AppSpawnReqMsgNode *)reqHandle;
40969570cc8Sopenharmony_ci    APPSPAWN_CHECK_ONLY_EXPER(reqNode != NULL, return APPSPAWN_ARG_INVALID);
41069570cc8Sopenharmony_ci    APPSPAWN_CHECK(reqNode->msgFlags != NULL, return APPSPAWN_ARG_INVALID, "No msg flags tlv ");
41169570cc8Sopenharmony_ci    APPSPAWN_CHECK(flagIndex < MAX_FLAGS_INDEX, return APPSPAWN_ARG_INVALID,
41269570cc8Sopenharmony_ci        "Invalid msg app flags %{public}d", flagIndex);
41369570cc8Sopenharmony_ci    if (!reqNode->isAsan &&
41469570cc8Sopenharmony_ci        (flagIndex == APP_FLAGS_UBSAN_ENABLED || flagIndex == APP_FLAGS_ASANENABLED ||
41569570cc8Sopenharmony_ci        flagIndex == APP_FLAGS_TSAN_ENABLED || flagIndex == APP_FLAGS_HWASAN_ENABLED ||
41669570cc8Sopenharmony_ci        (flagIndex == APP_FLAGS_COLD_BOOT && CheckEnabled("startup.appspawn.cold.boot", "true")))) {
41769570cc8Sopenharmony_ci        reqNode->isAsan = 1;
41869570cc8Sopenharmony_ci    }
41969570cc8Sopenharmony_ci
42069570cc8Sopenharmony_ci    return SetAppSpawnMsgFlags(reqNode->msgFlags, flagIndex);
42169570cc8Sopenharmony_ci}
42269570cc8Sopenharmony_ci
42369570cc8Sopenharmony_ciint AppSpawnReqMsgAddExtInfo(AppSpawnReqMsgHandle reqHandle, const char *name, const uint8_t *value, uint32_t valueLen)
42469570cc8Sopenharmony_ci{
42569570cc8Sopenharmony_ci    AppSpawnReqMsgNode *reqNode = (AppSpawnReqMsgNode *)reqHandle;
42669570cc8Sopenharmony_ci    APPSPAWN_CHECK_ONLY_EXPER(reqNode != NULL, return APPSPAWN_ARG_INVALID);
42769570cc8Sopenharmony_ci    int ret = CheckInputString("check name", name, APPSPAWN_TLV_NAME_LEN);
42869570cc8Sopenharmony_ci    APPSPAWN_CHECK_ONLY_EXPER(ret == 0, return ret);
42969570cc8Sopenharmony_ci    APPSPAWN_CHECK(value != NULL && valueLen <= EXTRAINFO_TOTAL_LENGTH_MAX && valueLen > 0,
43069570cc8Sopenharmony_ci        return APPSPAWN_ARG_INVALID, "Invalid ext value ");
43169570cc8Sopenharmony_ci
43269570cc8Sopenharmony_ci    APPSPAWN_LOGV("AppSpawnReqMsgAddExtInfo name %{public}s", name);
43369570cc8Sopenharmony_ci    AppSpawnAppData data[1] = {};  // 1 max data count
43469570cc8Sopenharmony_ci    data[0].data = (uint8_t *)value;
43569570cc8Sopenharmony_ci    data[0].dataLen = valueLen;
43669570cc8Sopenharmony_ci    return AddAppDataEx(reqNode, name, data);  // 2 max count
43769570cc8Sopenharmony_ci}
43869570cc8Sopenharmony_ci
43969570cc8Sopenharmony_ciint AppSpawnReqMsgAddStringInfo(AppSpawnReqMsgHandle reqHandle, const char *name, const char *value)
44069570cc8Sopenharmony_ci{
44169570cc8Sopenharmony_ci    AppSpawnReqMsgNode *reqNode = (AppSpawnReqMsgNode *)reqHandle;
44269570cc8Sopenharmony_ci    APPSPAWN_CHECK_ONLY_EXPER(reqNode != NULL, return APPSPAWN_ARG_INVALID);
44369570cc8Sopenharmony_ci    int ret = CheckInputString("check name", name, APPSPAWN_TLV_NAME_LEN);
44469570cc8Sopenharmony_ci    APPSPAWN_CHECK_ONLY_EXPER(ret == 0, return ret);
44569570cc8Sopenharmony_ci    ret = CheckInputString(name, value, EXTRAINFO_TOTAL_LENGTH_MAX);
44669570cc8Sopenharmony_ci    APPSPAWN_CHECK_ONLY_EXPER(ret == 0, return ret);
44769570cc8Sopenharmony_ci
44869570cc8Sopenharmony_ci    APPSPAWN_LOGV("AppSpawnReqMsgAddStringInfo name %{public}s", name);
44969570cc8Sopenharmony_ci    AppSpawnAppData data[1] = {};  // 1 max data count
45069570cc8Sopenharmony_ci    data[0].data = (uint8_t *)value;
45169570cc8Sopenharmony_ci    data[0].dataLen = strlen(value);
45269570cc8Sopenharmony_ci    data[0].dataType = DATA_TYPE_STRING;
45369570cc8Sopenharmony_ci    return AddAppDataEx(reqNode, name, data);  // 2 max count
45469570cc8Sopenharmony_ci}
45569570cc8Sopenharmony_ci
45669570cc8Sopenharmony_ciint AppSpawnReqMsgAddPermission(AppSpawnReqMsgHandle reqHandle, const char *permission)
45769570cc8Sopenharmony_ci{
45869570cc8Sopenharmony_ci    AppSpawnReqMsgNode *reqNode = (AppSpawnReqMsgNode *)reqHandle;
45969570cc8Sopenharmony_ci    APPSPAWN_CHECK_ONLY_EXPER(reqNode != NULL, return APPSPAWN_ARG_INVALID);
46069570cc8Sopenharmony_ci    APPSPAWN_CHECK(permission != NULL, return APPSPAWN_ARG_INVALID, "Invalid permission ");
46169570cc8Sopenharmony_ci    APPSPAWN_CHECK(reqNode->permissionFlags != NULL, return APPSPAWN_ARG_INVALID, "No permission tlv ");
46269570cc8Sopenharmony_ci
46369570cc8Sopenharmony_ci    int32_t maxIndex = GetMaxPermissionIndex(NULL);
46469570cc8Sopenharmony_ci    int index = GetPermissionIndex(NULL, permission);
46569570cc8Sopenharmony_ci    APPSPAWN_CHECK(index >= 0 && index < maxIndex,
46669570cc8Sopenharmony_ci        return APPSPAWN_PERMISSION_NOT_SUPPORT, "Invalid permission %{public}s", permission);
46769570cc8Sopenharmony_ci    APPSPAWN_LOGV("AddPermission index %{public}d name %{public}s", index, permission);
46869570cc8Sopenharmony_ci    int ret = SetAppSpawnMsgFlags(reqNode->permissionFlags, index);
46969570cc8Sopenharmony_ci    APPSPAWN_CHECK(ret == 0, return ret, "Invalid permission %{public}s", permission);
47069570cc8Sopenharmony_ci    return 0;
47169570cc8Sopenharmony_ci}
47269570cc8Sopenharmony_ci
47369570cc8Sopenharmony_ciint AppSpawnReqMsgSetAppDomainInfo(AppSpawnReqMsgHandle reqHandle, uint32_t hapFlags, const char *apl)
47469570cc8Sopenharmony_ci{
47569570cc8Sopenharmony_ci    AppSpawnReqMsgNode *reqNode = (AppSpawnReqMsgNode *)reqHandle;
47669570cc8Sopenharmony_ci    APPSPAWN_CHECK_ONLY_EXPER(reqNode != NULL, return APPSPAWN_ARG_INVALID);
47769570cc8Sopenharmony_ci    int ret = CheckInputString("TLV_DOMAIN_INFO", apl, APP_APL_MAX_LEN);
47869570cc8Sopenharmony_ci    APPSPAWN_CHECK_ONLY_EXPER(ret == 0, return ret);
47969570cc8Sopenharmony_ci
48069570cc8Sopenharmony_ci    AppSpawnMsgDomainInfo msgDomainInfo;
48169570cc8Sopenharmony_ci    msgDomainInfo.hapFlags = hapFlags;
48269570cc8Sopenharmony_ci    AppSpawnAppData data[MAX_DATA_IN_TLV] = {};
48369570cc8Sopenharmony_ci    data[0].data = (uint8_t *)&msgDomainInfo;
48469570cc8Sopenharmony_ci    data[0].dataLen = sizeof(AppSpawnMsgDomainInfo);
48569570cc8Sopenharmony_ci    data[1].data = (uint8_t *)apl;
48669570cc8Sopenharmony_ci    data[1].dataLen = strlen(apl);
48769570cc8Sopenharmony_ci    data[1].dataType = DATA_TYPE_STRING;
48869570cc8Sopenharmony_ci    return AddAppData(reqNode, TLV_DOMAIN_INFO, data, MAX_DATA_IN_TLV, "TLV_DOMAIN_INFO");
48969570cc8Sopenharmony_ci}
49069570cc8Sopenharmony_ci
49169570cc8Sopenharmony_ciint AppSpawnReqMsgSetAppInternetPermissionInfo(AppSpawnReqMsgHandle reqHandle, uint8_t allow, uint8_t setAllow)
49269570cc8Sopenharmony_ci{
49369570cc8Sopenharmony_ci    AppSpawnReqMsgNode *reqNode = (AppSpawnReqMsgNode *)reqHandle;
49469570cc8Sopenharmony_ci    APPSPAWN_CHECK_ONLY_EXPER(reqNode != NULL, return APPSPAWN_ARG_INVALID);
49569570cc8Sopenharmony_ci
49669570cc8Sopenharmony_ci    AppSpawnMsgInternetInfo info = {};
49769570cc8Sopenharmony_ci    info.allowInternet = allow;
49869570cc8Sopenharmony_ci    info.setAllowInternet = setAllow;
49969570cc8Sopenharmony_ci    AppSpawnAppData data[MAX_DATA_IN_TLV] = {};
50069570cc8Sopenharmony_ci    data[0].data = (uint8_t *)&info;
50169570cc8Sopenharmony_ci    data[0].dataLen = sizeof(AppSpawnMsgInternetInfo);
50269570cc8Sopenharmony_ci    return AddAppData(reqNode, TLV_INTERNET_INFO, data, 1, "TLV_INTERNET_INFO");
50369570cc8Sopenharmony_ci}
50469570cc8Sopenharmony_ci
50569570cc8Sopenharmony_ciint AppSpawnReqMsgSetAppOwnerId(AppSpawnReqMsgHandle reqHandle, const char *ownerId)
50669570cc8Sopenharmony_ci{
50769570cc8Sopenharmony_ci    AppSpawnReqMsgNode *reqNode = (AppSpawnReqMsgNode *)reqHandle;
50869570cc8Sopenharmony_ci    APPSPAWN_CHECK_ONLY_EXPER(reqNode != NULL, return APPSPAWN_ARG_INVALID);
50969570cc8Sopenharmony_ci    int ret = CheckInputString("TLV_OWNER_INFO", ownerId, APP_OWNER_ID_LEN);
51069570cc8Sopenharmony_ci    APPSPAWN_CHECK_ONLY_EXPER(ret == 0, return ret);
51169570cc8Sopenharmony_ci
51269570cc8Sopenharmony_ci    AppSpawnAppData data[MAX_DATA_IN_TLV] = {};
51369570cc8Sopenharmony_ci    data[0].data = (uint8_t *)ownerId;
51469570cc8Sopenharmony_ci    data[0].dataLen = strlen(ownerId);
51569570cc8Sopenharmony_ci    data[0].dataType = DATA_TYPE_STRING;
51669570cc8Sopenharmony_ci    return AddAppData(reqNode, TLV_OWNER_INFO, data, 1, "TLV_OWNER_INFO");
51769570cc8Sopenharmony_ci}
51869570cc8Sopenharmony_ci
51969570cc8Sopenharmony_ciint AppSpawnReqMsgSetAppAccessToken(AppSpawnReqMsgHandle reqHandle, uint64_t accessTokenIdEx)
52069570cc8Sopenharmony_ci{
52169570cc8Sopenharmony_ci    AppSpawnReqMsgNode *reqNode = (AppSpawnReqMsgNode *)reqHandle;
52269570cc8Sopenharmony_ci    APPSPAWN_CHECK_ONLY_EXPER(reqNode != NULL, return APPSPAWN_ARG_INVALID);
52369570cc8Sopenharmony_ci
52469570cc8Sopenharmony_ci    AppSpawnAppData data[MAX_DATA_IN_TLV] = {};
52569570cc8Sopenharmony_ci    data[0].data = (uint8_t *)&accessTokenIdEx;
52669570cc8Sopenharmony_ci    data[0].dataLen = sizeof(accessTokenIdEx);
52769570cc8Sopenharmony_ci    return AddAppData(reqNode, TLV_ACCESS_TOKEN_INFO, data, 1, "TLV_ACCESS_TOKEN_INFO");
52869570cc8Sopenharmony_ci}
52969570cc8Sopenharmony_ci
53069570cc8Sopenharmony_ciint AppSpawnTerminateMsgCreate(pid_t pid, AppSpawnReqMsgHandle *reqHandle)
53169570cc8Sopenharmony_ci{
53269570cc8Sopenharmony_ci    APPSPAWN_CHECK(reqHandle != NULL, return APPSPAWN_ARG_INVALID, "Invalid request handle");
53369570cc8Sopenharmony_ci    AppSpawnReqMsgNode *reqNode = CreateAppSpawnReqMsg(MSG_GET_RENDER_TERMINATION_STATUS, "terminate-process");
53469570cc8Sopenharmony_ci    APPSPAWN_CHECK(reqNode != NULL, return APPSPAWN_SYSTEM_ERROR, "Failed to create msg node");
53569570cc8Sopenharmony_ci
53669570cc8Sopenharmony_ci    AppSpawnAppData data[MAX_DATA_IN_TLV] = {};
53769570cc8Sopenharmony_ci    data[0].data = (uint8_t *)&pid;
53869570cc8Sopenharmony_ci    data[0].dataLen = sizeof(pid);
53969570cc8Sopenharmony_ci    int ret = AddAppData(reqNode, TLV_RENDER_TERMINATION_INFO, data, 1, "TLV_RENDER_TERMINATION_INFO");
54069570cc8Sopenharmony_ci    APPSPAWN_CHECK_ONLY_EXPER(ret == 0, DeleteAppSpawnReqMsg(reqNode);
54169570cc8Sopenharmony_ci        return ret);
54269570cc8Sopenharmony_ci    *reqHandle = (AppSpawnReqMsgHandle)(reqNode);
54369570cc8Sopenharmony_ci    return 0;
54469570cc8Sopenharmony_ci}
54569570cc8Sopenharmony_ci
54669570cc8Sopenharmony_ciint AppSpawnClientAddPermission(AppSpawnClientHandle handle, AppSpawnReqMsgHandle reqHandle, const char *permission)
54769570cc8Sopenharmony_ci{
54869570cc8Sopenharmony_ci    AppSpawnReqMsgMgr *reqMgr = (AppSpawnReqMsgMgr *)handle;
54969570cc8Sopenharmony_ci    APPSPAWN_CHECK(reqMgr != NULL, return APPSPAWN_ARG_INVALID, "Invalid reqMgr");
55069570cc8Sopenharmony_ci    AppSpawnReqMsgNode *reqNode = (AppSpawnReqMsgNode *)reqHandle;
55169570cc8Sopenharmony_ci    APPSPAWN_CHECK_ONLY_EXPER(reqNode != NULL, return APPSPAWN_ARG_INVALID);
55269570cc8Sopenharmony_ci    APPSPAWN_CHECK(permission != NULL, return APPSPAWN_ARG_INVALID, "Invalid permission ");
55369570cc8Sopenharmony_ci    APPSPAWN_CHECK(reqNode->permissionFlags != NULL, return APPSPAWN_ARG_INVALID, "No permission tlv ");
55469570cc8Sopenharmony_ci
55569570cc8Sopenharmony_ci    int32_t maxIndex = GetMaxPermissionIndex(handle);
55669570cc8Sopenharmony_ci    int index = GetPermissionIndex(handle, permission);
55769570cc8Sopenharmony_ci    APPSPAWN_CHECK(index >= 0 && index < maxIndex,
55869570cc8Sopenharmony_ci        return APPSPAWN_PERMISSION_NOT_SUPPORT, "Invalid permission %{public}s", permission);
55969570cc8Sopenharmony_ci    APPSPAWN_LOGV("add permission index %{public}d name %{public}s", index, permission);
56069570cc8Sopenharmony_ci    int ret = SetAppSpawnMsgFlags(reqNode->permissionFlags, index);
56169570cc8Sopenharmony_ci    APPSPAWN_CHECK(ret == 0, return ret, "Invalid permission %{public}s", permission);
56269570cc8Sopenharmony_ci    return 0;
56369570cc8Sopenharmony_ci}
564