distributedschedule_samgr_lite/samgr_server/source/samgr_server.c
进程获取功能,用于获取该进程的特征值
static int32 ProcGetFeature(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) {
IpcIoPushInt32(reply, EC_INVALID);
return EC_INVALID;
}
char *feature = IpcIoPopBool(req) ? NULL : (char *)IpcIoPopString(req, &len);
MUTEX_Lock(server->mtx); //互斥锁
*identity = SASTORA_Find(&server->store, service, feature);
//若身份满足如下条件,则提示找不到该功能,报错
if (identity->handle == INVALID_INDEX) {
MUTEX_Unlock(server->mtx);
HILOG_DEBUG(HILOG_MODULE_SAMGR, "Cannot Find Feature<%s, %s> id<%d, %d> ret:%d",
service, feature, identity->handle, identity->token, EC_NOSERVICE);
return EC_NOSERVICE;
}
PidHandle providerPid = SASTORA_FindPidHandleByIpcHandle(&server->store, identity->handle);
MUTEX_Unlock(server->mtx);
//若提供的pid为以下情况,则无法找到pid处理的id
if (providerPid.pid == INVALID_INDEX || providerPid.uid == INVALID_INDEX) {
HILOG_DEBUG(HILOG_MODULE_SAMGR, "Cannot Find PidHandle<%s, %s> id<%d, %d> ret:%d",
service, feature, identity->handle, identity->token, EC_FAILURE);
return EC_FAILURE;
}
AuthParams authParams = {
.providerService = service,
.providerfeature = feature,
.consumerPid = GetCallingPid(origin),
.consumerUid = GetCallingUid(origin),
.providerPid = providerPid.pid,
.providerUid = providerPid.uid
};
int isAuth = g_server.ipcAuth->IsCommunicationAllowed(authParams);
HILOG_DEBUG(HILOG_MODULE_SAMGR, "Judge Auth<%s, %s> ret:%d", service, feature, isAuth);
int32 ret = (isAuth == EC_SUCCESS) ? AddServiceAccess(*identity, GetCallingTid(origin)) : EC_PERMISSION;
HILOG_DEBUG(HILOG_MODULE_SAMGR, "Find Feature<%s, %s> id<%d, %d> ret:%d",
service, feature, identity->handle, identity->token, ret);
return ret;
}
进程特征,该函数用于检测特征值是否设置成功
static int ProcFeature(SamgrServer *server, int32 option, void *origin, IpcIo *req, IpcIo *reply)
{
if (option != OP_PUT && option != OP_GET) {
IpcIoPushInt32(reply, EC_INVALID);
return EC_INVALID;
}
if (g_server.ipcAuth == NULL) {
g_server.ipcAuth = GetIpcAuthInterface();
}
if (g_server.ipcAuth == NULL) {
IpcIoPushInt32(reply, EC_NOINIT);
return EC_NOINIT;
}
int ret = EC_SUCCESS;
SvcIdentity identity = {INVALID_INDEX, INVALID_INDEX, INVALID_INDEX};
if (option == OP_PUT) {
ret = ProcPutFeature(server, origin, req, reply, &identity);
}
if (ret != EC_SUCCESS) {
return ret;
}
if (option == OP_GET) {
ret = ProcGetFeature(server, origin, req, reply, &identity);
IpcIoPushInt32(reply, ret);
if (ret == EC_SUCCESS) {
IpcIoPushSvc(reply, &identity);
}
}
return ret;
}
程序添加系统上限
static int32 ProcAddSysCap(SamgrServer *server, IpcIo *req)
{
size_t len = 0;
char *sysCap = (char *)IpcIoPopString(req, &len);
//若满足如下情况,则添加系统程序syscap无效
if (sysCap == NULL || len == 0 || len > MAX_SYSCAP_NAME_LEN) {
HILOG_ERROR(HILOG_MODULE_SAMGR, "ProcAddSysCap sysCap invalid");
return EC_INVALID;
}
MUTEX_Lock(server->sysCapMtx); //互斥锁
Vector *sysCapablitys = &(server->sysCapabilitys);
//使用指定的键检查元素的位置
int16 pos = VECTOR_FindByKey(sysCapablitys, (void *)sysCap); //此函数用于根据元素的键值检查元素
//vector表示指向该向量的<b>指针。
//key表示指向要检查的元素键值的指针。
if (pos < 0) {
MUTEX_Unlock(server->sysCapMtx); //互斥解锁
return EC_FAILURE;
}
SysCapImpl *serviceImpl = (SysCapImpl *)VECTOR_At(sysCapablitys, pos);
if (serviceImpl == NULL || serviceImpl->name == NULL) {
MUTEX_Unlock(server->sysCapMtx);
return EC_FAILURE;
}
serviceImpl->isRegister = TRUE;
MUTEX_Unlock(server->sysCapMtx); //互斥解锁
return EC_SUCCESS;
}
获取系统进程上限
static BOOL ProcGetSysCap(const SamgrServer *server, IpcIo *req)
{
size_t len = 0;
char *sysCap = (char *)IpcIoPopString(req, &len);
//若满足如下情况,则获取失效,返回报错
if (sysCap == NULL || len == 0 || len > MAX_SYSCAP_NAME_LEN) {
HILOG_ERROR(HILOG_MODULE_SAMGR, "ProcGetSysCap sysCap invalid");
return FALSE;
}
MUTEX_Lock(server->sysCapMtx); //互斥锁
Vector *sysCapablitys = &(server->sysCapabilitys);
int16 pos = VECTOR_FindByKey(sysCapablitys, (void *)sysCap); //使用指定的键检查元素的位置
if (pos < 0) {
MUTEX_Unlock(server->sysCapMtx); //互斥解锁
return FALSE;
}
SysCapImpl *serviceImpl = (SysCapImpl *)VECTOR_At(sysCapablitys, pos);
if (serviceImpl == NULL) {
MUTEX_Unlock(server->sysCapMtx);
return FALSE;
}
BOOL res = (serviceImpl->isRegister == TRUE);
MUTEX_Unlock(server->sysCapMtx);
return res;
}
该函数的功能是获取回复编号和下一个请求Idx,用于获取和回复当前的注册用户,同时获取下一个注册用户的信息
static int32 GetReplyNumAndNextReqIdx(const Vector *sysCapablitys, int32 startIdx, int32 *nextRequestIdx)
{
int32 registerNum = 0;
//获取向量中的有效元素数,不包括已设置为NULL的元素。
int32 size = VECTOR_Num(sysCapablitys); //此功能用于检查元素数量是否达到上限
//vector表示指向该向量的指针
int32 i = startIdx;
for (; i < size && registerNum < MAX_SYSCAP_NUM_PER_REPLY; i++) {
SysCapImpl *serviceImpl = (SysCapImpl *)VECTOR_At(sysCapablitys, i);
if (serviceImpl->isRegister == FALSE) {
continue;
}
registerNum++;
}
*nextRequestIdx = i;
return registerNum;
}
进程获取所有系统上限
void ProcGetAllSysCap(const SamgrServer *server, IpcIo *req, IpcIo *reply)
{
uint32_t startIdx = IpcIoPopUint32(req);
MUTEX_Lock(server->sysCapMtx);
Vector *sysCapablitys = &(server->sysCapabilitys);
int32 size = VECTOR_Num(sysCapablitys);
if (size == INVALID_INDEX) {
IpcIoPushInt32(reply, EC_FAILURE);
IpcIoPushBool(reply, TRUE);
IpcIoPushUint32(reply, startIdx);
IpcIoPushUint32(reply, 0);
MUTEX_Unlock(server->sysCapMtx);
return;
}
int32 nextRequestIdx = startIdx;
int32 replyNum = GetReplyNumAndNextReqIdx(sysCapablitys, startIdx, &nextRequestIdx);
HILOG_DEBUG(HILOG_MODULE_SAMGR, "ProcGetAllSysCap replyNum: %d, size: %d, startIdx: %d, nextRequestIdx: %d",
replyNum, size, startIdx, nextRequestIdx);
IpcIoPushInt32(reply, EC_SUCCESS);
// indicate is the last reply
//这是最后的答复
IpcIoPushBool(reply, nextRequestIdx == size);
// indicate is the next start idx
//指示是下一个开始idx
IpcIoPushUint32(reply, nextRequestIdx);
IpcIoPushUint32(reply, replyNum);
int32 cnt = 0;
int32 i = startIdx;
for (; i < size && cnt < replyNum; i++) {
SysCapImpl *serviceImpl = (SysCapImpl *)VECTOR_At(sysCapablitys, i);
if (serviceImpl->isRegister == FALSE) {
continue;
}
IpcIoPushString(reply, serviceImpl->name);
cnt++;
}
MUTEX_Unlock(server->sysCapMtx); //互斥解锁
}
测试进程的系统上限功能
static int ProcSysCap(SamgrServer *server, int32 option, void *origin, IpcIo *req, IpcIo *reply)
{
//若满足如下情况,则不给予权限
if (CanRequest(origin) == FALSE) {
HILOG_ERROR(HILOG_MODULE_SAMGR, "ProcSysCap no permission");
IpcIoPushInt32(reply, EC_PERMISSION);
return EC_PERMISSION;
}
if (option != OP_PUT && option != OP_GET && option != OP_ALL) {
IpcIoPushInt32(reply, EC_INVALID);
return EC_INVALID;
}
//启动获取进程的系统上限功能
HILOG_DEBUG(HILOG_MODULE_SAMGR, "ProcSysCap option: %d begin", option);
if (option == OP_PUT) {
int32 ret = ProcAddSysCap(server, req);
IpcIoPushInt32(reply, ret);
} else if (option == OP_GET) {
BOOL ret = ProcGetSysCap(server, req);
IpcIoPushInt32(reply, EC_SUCCESS);
IpcIoPushBool(reply, ret);
} else if (option == OP_ALL) {
ProcGetAllSysCap(server, req, reply);
} else {
HILOG_WARN(HILOG_MODULE_SAMGR, "ProcSysCap error option: %d", option);
IpcIoPushInt32(reply, EC_INVALID);
return EC_INVALID;
}
HILOG_DEBUG(HILOG_MODULE_SAMGR, "ProcSysCap end");
return EC_SUCCESS;
}