OpenHarmony模块二下文件samgr_server解析(3)

distributedschedule_samgr_lite/samgr_server/source/samgr_server.c

本篇博客将继续完成上次未完成的工作,继续介绍该文件下的后续代码

该函数的功能是作为服务器代理接收信息然后调用相关函数

static int32 Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply)
{
    SamgrServer *server = GET_OBJECT(iProxy, SamgrServer, iUnknown);
    int32 resource = IpcIoPopUint32(req);
    int32 option = IpcIoPopUint32(req);
    //若消息满足如下情况,则视为无效消息
    if (server == NULL || resource >= RES_BUTT || resource < 0 || g_functions[resource] == NULL) {
        HILOG_ERROR(HILOG_MODULE_SAMGR, "Invalid Msg<%d, %d, %d>", resource, option, funcId);
        return EC_INVALID;
    }
    return g_functions[resource](server, option, origin, req, reply);
}

进程终端,用于设置进程结束的节点

static int ProcEndpoint(SamgrServer *server, int32 option, void *origin, IpcIo *req, IpcIo *reply) 
{           
    if (option != OP_POST) {    //满足这个条件时,返回EC_FAILURE
        IpcIoPushUint32(reply, (uint32)INVALID_INDEX);
        return EC_FAILURE;
    }
    pid_t pid = GetCallingPid(origin);
    PidHandle handle;
    MUTEX_Lock(server->mtx);    //互斥锁
    int index = SASTORA_FindHandleByPid(&g_server.store, pid, &handle);
    if (index == INVALID_INDEX) {
        SvcIdentity identity = {(uint32)INVALID_INDEX, (uint32)INVALID_INDEX, (uint32)INVALID_INDEX};
        (void)GenServiceHandle(&identity, GetCallingTid(origin));
#ifdef __LINUX__
        BinderAcquire(g_server.samgr->context, identity.handle);
#endif

        handle.pid = pid;
        handle.uid = GetCallingUid(origin);
        handle.handle = identity.handle;
        handle.deadId = INVALID_INDEX;
        (void)SASTORA_SaveHandleByPid(&server->store, handle);  //通过Pid保存处理
        (void)UnregisterDeathCallback(identity, handle.deadId); //注销死亡回调
        //注册死亡回调
        (void)RegisterDeathCallback(server->samgr->context, identity, OnEndpointExit, (void*)((uintptr_t)pid),
                                    &handle.deadId);
    }
    MUTEX_Unlock(server->mtx);  //互斥解锁
    IpcIoPushUint32(reply, handle.handle);
    //寄存器端点
    HILOG_INFO(HILOG_MODULE_SAMGR, "Register Endpoint<%d, %d, %d>", handle.pid, handle.handle, handle.deadId);
    return EC_SUCCESS;
}

进程输出特性,用于设置进程的特征值

static int32 ProcPutFeature(SamgrServer *server, const void *origin, IpcIo *req, IpcIo *reply, SvcIdentity *identity)
{
    size_t len = 0;
    char *service = (char *)IpcIoPopString(req, &len);
    if (service == NULL || len == 0) {  //满足条件时,返回EC_INVALID
        IpcIoPushInt32(reply, EC_INVALID);
        return EC_INVALID;
    }
    pid_t pid = GetCallingPid(origin);
    uid_t uid = GetCallingUid(origin);
    char *feature = IpcIoPopBool(req) ? NULL : (char *)IpcIoPopString(req, &len);
    MUTEX_Lock(server->mtx);    //互斥锁
    PidHandle handle;
    int index = SASTORA_FindHandleByUidPid(&server->store, uid, pid, &handle);
    //若满足如下情况,则端点未注册
    if (index == INVALID_INDEX) {
        MUTEX_Unlock(server->mtx);
        HILOG_ERROR(HILOG_MODULE_SAMGR, "Endpoint[%d] is not register", pid);
        IpcIoPushInt32(reply, EC_NOSERVICE);
        return EC_NOSERVICE;
    }
    *identity = SASTORA_Find(&server->store, service, feature);
    if (identity->handle != INVALID_INDEX && identity->handle != handle.handle) {
        MUTEX_Unlock(server->mtx);
        IpcIoPushInt32(reply, EC_INVALID);
        return EC_INVALID;
    }
    identity->token = IpcIoPopUint32(req);
    identity->handle = handle.handle;

    PolicyTrans *policy = NULL;
    RegParams regParams = {service, feature, handle.uid, handle.pid};
    uint32 policyNum = 0;
    int ret = g_server.ipcAuth->GetCommunicationStrategy(regParams, &policy, &policyNum);
    if (ret != EC_SUCCESS || policy == NULL) {
        MUTEX_Unlock(server->mtx);  //互斥解锁
        SAMGR_Free(policy);
        //远程Get通信策略
        HILOG_DEBUG(HILOG_MODULE_SAMGR, "Remote Get Communication Strategy<%s, %s> No Permission<%d>!",
                    service, feature, ret);
        IpcIoPushInt32(reply, EC_PERMISSION);
        return EC_PERMISSION;
    }

    ret = SASTORA_Save(&server->store, service, feature, identity);
    MUTEX_Unlock(server->mtx);
    //寄存器特征
    HILOG_DEBUG(HILOG_MODULE_SAMGR, "Register Feature<%s, %s> pid<%d>, id<%d, %d> ret:%d",
                service, feature, pid, identity->handle, identity->token, ret);
    TransmitPolicy(ret, identity, reply, policy, policyNum);
    SAMGR_Free(policy);
    return ret;
}

传送服务信息的策略

static void TransmitPolicy(int ret, const SvcIdentity* identity, IpcIo *reply,
                           const PolicyTrans *policy, uint32 policyNum)
{
    if (identity == NULL || reply == NULL || policy == NULL) {
        IpcIoPushInt32(reply, EC_INVALID);
        return;
    }
    if (ret != EC_SUCCESS) {
        IpcIoPushInt32(reply, ret);
        return;
    }
    IpcIoPushInt32(reply, ret);
    IpcIoPushSvc(reply, identity);
    IpcIoPushUint32(reply, policyNum);
    uint32 i;
    for (i = 0; i < policyNum; i++) {
        IpcIoPushInt32(reply, policy[i].type);
        switch (policy[i].type) {
            case RANGE:
                IpcIoPushInt32(reply, policy[i].uidMin);
                IpcIoPushInt32(reply, policy[i].uidMax);
                break;
            case FIXED:
                TransmitFixedPolicy(reply, policy[i]);
                break;
            case BUNDLENAME:
                IpcIoPushInt32(reply, policy[i].fixedUid[0]);
                break;
            default:
                break;
        }
    }
}

传输固定策略

static void TransmitFixedPolicy(IpcIo *reply, PolicyTrans policy)
{
    if (reply == NULL) {
        return;
    }
    uint32 i;
    for (i = 0; i < UID_SIZE; i++) {
        IpcIoPushInt32(reply, policy.fixedUid[i]);
    }
}

该文件还有很多代码,剩下的代码我将在后续博客继续介绍

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值