提交 a2fd53ab 编写于 作者: 1 18392170496

file format

Sig:SIG_ApplicationFramework
Feature or Bugfix:Bugfix
Binary Source:No
Signed-off-by: N@ma-gentang <magentang3@huawei.com>
Signed-off-by: N18392170496 <magentang3@huawei.com>
上级 ddc848ba
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_IPC_INVOKER_H
#define OHOS_IPC_INVOKER_H
#include "iremote_invoker.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
RemoteInvoker *GetIpcInvoker(void);
void DeinitIpcInvoker(RemoteInvoker *invoker);
#ifdef __cplusplus
}
#endif /* __cplusplus */
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_IPC_INVOKER_H
#define OHOS_IPC_INVOKER_H
#include "iremote_invoker.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
RemoteInvoker *GetIpcInvoker(void);
void DeinitIpcInvoker(RemoteInvoker *invoker);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* OHOS_IPC_INVOKER_H */
\ No newline at end of file
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _UAPI_LINUX_BINDER_H
#define _UAPI_LINUX_BINDER_H
#include <stdint.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#define B_PACK_CHARS(c1, c2, c3, c4) ((((c1) << 24)) | (((c2) << 16)) | (((c3) << 8)) | (c4))
#define B_TYPE_LARGE 0x85
#define BINDER_IPC_32BIT
enum {
BINDER_TYPE_BINDER = B_PACK_CHARS('s', 'b', '*', B_TYPE_LARGE),
BINDER_TYPE_REMOTE_BINDER = B_PACK_CHARS('r', 'b', '*', B_TYPE_LARGE),
BINDER_TYPE_WEAK_BINDER = B_PACK_CHARS('w', 'b', '*', B_TYPE_LARGE),
BINDER_TYPE_HANDLE = B_PACK_CHARS('s', 'h', '*', B_TYPE_LARGE),
BINDER_TYPE_REMOTE_HANDLE = B_PACK_CHARS('r', 'h', '*', B_TYPE_LARGE),
BINDER_TYPE_WEAK_HANDLE = B_PACK_CHARS('w', 'h', '*', B_TYPE_LARGE),
BINDER_TYPE_FD = B_PACK_CHARS('f', 'd', '*', B_TYPE_LARGE),
BINDER_TYPE_FDA = B_PACK_CHARS('f', 'd', 'a', B_TYPE_LARGE),
BINDER_TYPE_PTR = B_PACK_CHARS('p', 't', '*', B_TYPE_LARGE),
};
enum {
FLAT_BINDER_FLAG_PRIORITY_MASK = 0xff,
FLAT_BINDER_FLAG_ACCEPTS_FDS = 0x100,
};
#ifdef BINDER_IPC_32BIT
typedef __u32 binder_size_t;
typedef __u32 binder_uintptr_t;
#else
typedef __u64 binder_size_t;
typedef __u64 binder_uintptr_t;
#endif
struct binder_object_header {
__u32 type;
};
struct flat_binder_object {
__u32 type;
__u32 flags;
union {
binder_uintptr_t binder;
__u32 handle;
};
binder_uintptr_t cookie;
};
struct binder_fd_object {
struct binder_object_header hdr;
__u32 pad_flags;
union {
binder_uintptr_t pad_binder;
__u32 fd;
};
binder_uintptr_t cookie;
};
struct binder_buffer_object {
struct binder_object_header hdr;
__u32 flags;
binder_uintptr_t buffer;
binder_size_t length;
binder_size_t parent;
binder_size_t parent_offset;
};
enum {
BINDER_BUFFER_FLAG_HAS_PARENT = 0x01,
};
struct binder_fd_array_object {
struct binder_object_header hdr;
__u32 pad;
binder_size_t num_fds;
binder_size_t parent;
binder_size_t parent_offset;
};
struct binder_write_read {
binder_size_t write_size;
binder_size_t write_consumed;
binder_uintptr_t write_buffer;
binder_size_t read_size;
binder_size_t read_consumed;
binder_uintptr_t read_buffer;
};
struct binder_version {
__s32 protocol_version;
};
#ifdef BINDER_IPC_32BIT
#define BINDER_CURRENT_PROTOCOL_VERSION 7
#else
#define BINDER_CURRENT_PROTOCOL_VERSION 8
#endif
struct binder_node_debug_info {
binder_uintptr_t ptr;
binder_uintptr_t cookie;
__u32 has_strong_ref;
__u32 has_weak_ref;
};
struct binder_ptr_count {
binder_uintptr_t ptr;
uint32_t count;
};
#define BINDER_WRITE_READ _IOWR('b', 1, struct binder_write_read)
#define BINDER_SET_IDLE_TIMEOUT _IOW('b', 3, __s64)
#define BINDER_SET_MAX_THREADS _IOW('b', 5, __u32)
#define BINDER_SET_IDLE_PRIORITY _IOW('b', 6, __s32)
#define BINDER_SET_CONTEXT_MGR _IOW('b', 7, __s32)
#define BINDER_THREAD_EXIT _IOW('b', 8, __s32)
#define BINDER_VERSION _IOWR('b', 9, struct binder_version)
#define BINDER_GET_NODE_DEBUG_INFO _IOWR('b', 11, struct binder_node_debug_info)
#define BINDER_GET_NODE_REFCOUNT _IOWR('b', 17, struct binder_ptr_count)
enum transaction_flags {
TF_ONE_WAY = 0x01,
TF_ROOT_OBJECT = 0x04,
TF_STATUS_CODE = 0x08,
TF_ACCEPT_FDS = 0x10,
};
struct binder_transaction_data {
union {
__u32 handle;
binder_uintptr_t ptr;
} target;
binder_uintptr_t cookie;
__u32 code;
__u32 flags;
pid_t sender_pid;
uid_t sender_euid;
binder_size_t data_size;
binder_size_t offsets_size;
union {
struct {
binder_uintptr_t buffer;
binder_uintptr_t offsets;
} ptr;
__u8 buf[8];
} data;
};
struct binder_transaction_data_sg {
struct binder_transaction_data transaction_data;
binder_size_t buffers_size;
};
struct binder_ptr_cookie {
binder_uintptr_t ptr;
binder_uintptr_t cookie;
};
struct binder_handle_cookie {
__u32 handle;
binder_uintptr_t cookie;
}__attribute__((__packed__));
struct binder_pri_desc {
__s32 priority;
__u32 desc;
};
struct binder_pri_ptr_cookie {
__s32 priority;
binder_uintptr_t ptr;
binder_uintptr_t cookie;
};
enum binder_driver_return_protocol {
BR_ERROR = _IOR('r', 0, __s32),
BR_OK = _IO('r', 1),
BR_TRANSACTION = _IOR('r', 2, struct binder_transaction_data),
BR_REPLY = _IOR('r', 3, struct binder_transaction_data),
BR_ACQUIRE_RESULT = _IOR('r', 4, __s32),
BR_DEAD_REPLY = _IO('r', 5),
BR_TRANSACTION_COMPLETE = _IO('r', 6),
BR_INCREFS = _IOR('r', 7, struct binder_ptr_cookie),
BR_ACQUIRE = _IOR('r', 8, struct binder_ptr_cookie),
BR_RELEASE = _IOR('r', 9, struct binder_ptr_cookie),
BR_DECREFS = _IOR('r', 10, struct binder_ptr_cookie),
BR_ATTEMPT_ACQUIRE = _IOR('r', 11, struct binder_pri_ptr_cookie),
BR_NOOP = _IO('r', 12),
BR_SPAWN_LOOPER = _IO('r', 13),
BR_FINISHED = _IO('r', 14),
BR_DEAD_BINDER = _IOR('r', 15, binder_uintptr_t),
BR_CLEAR_DEATH_NOTIFICATION_DONE = _IOR('r', 16, binder_uintptr_t),
BR_FAILED_REPLY = _IO('r', 17),
BR_RELEASE_NODE = _IO('r', 18),
};
enum binder_driver_command_protocol {
BC_TRANSACTION = _IOW('c', 0, struct binder_transaction_data),
BC_REPLY = _IOW('c', 1, struct binder_transaction_data),
BC_ACQUIRE_RESULT = _IOW('c', 2, __s32),
BC_FREE_BUFFER = _IOW('c', 3, binder_uintptr_t),
BC_INCREFS = _IOW('c', 4, __u32),
BC_ACQUIRE = _IOW('c', 5, __u32),
BC_RELEASE = _IOW('c', 6, __u32),
BC_DECREFS = _IOW('c', 7, __u32),
BC_INCREFS_DONE = _IOW('c', 8, struct binder_ptr_cookie),
BC_ACQUIRE_DONE = _IOW('c', 9, struct binder_ptr_cookie),
BC_ATTEMPT_ACQUIRE = _IOW('c', 10, struct binder_pri_desc),
BC_REGISTER_LOOPER = _IO('c', 11),
BC_ENTER_LOOPER = _IO('c', 12),
BC_EXIT_LOOPER = _IO('c', 13),
BC_REQUEST_DEATH_NOTIFICATION = _IOW('c', 14, struct binder_handle_cookie),
BC_CLEAR_DEATH_NOTIFICATION = _IOW('c', 15, struct binder_handle_cookie),
BC_DEAD_BINDER_DONE = _IOW('c', 16, binder_uintptr_t),
BC_TRANSACTION_SG = _IOW('c', 17, struct binder_transaction_data_sg),
BC_REPLY_SG = _IOW('c', 18, struct binder_transaction_data_sg),
BC_RELEASE_NODE = _IOW('c', 19, binder_uintptr_t),
};
#endif
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _UAPI_LINUX_BINDER_H
#define _UAPI_LINUX_BINDER_H
#include <stdint.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#define B_PACK_CHARS(c1, c2, c3, c4) ((((c1) << 24)) | (((c2) << 16)) | (((c3) << 8)) | (c4))
#define B_TYPE_LARGE 0x85
#define BINDER_IPC_32BIT
enum {
BINDER_TYPE_BINDER = B_PACK_CHARS('s', 'b', '*', B_TYPE_LARGE),
BINDER_TYPE_REMOTE_BINDER = B_PACK_CHARS('r', 'b', '*', B_TYPE_LARGE),
BINDER_TYPE_WEAK_BINDER = B_PACK_CHARS('w', 'b', '*', B_TYPE_LARGE),
BINDER_TYPE_HANDLE = B_PACK_CHARS('s', 'h', '*', B_TYPE_LARGE),
BINDER_TYPE_REMOTE_HANDLE = B_PACK_CHARS('r', 'h', '*', B_TYPE_LARGE),
BINDER_TYPE_WEAK_HANDLE = B_PACK_CHARS('w', 'h', '*', B_TYPE_LARGE),
BINDER_TYPE_FD = B_PACK_CHARS('f', 'd', '*', B_TYPE_LARGE),
BINDER_TYPE_FDA = B_PACK_CHARS('f', 'd', 'a', B_TYPE_LARGE),
BINDER_TYPE_PTR = B_PACK_CHARS('p', 't', '*', B_TYPE_LARGE),
};
enum {
FLAT_BINDER_FLAG_PRIORITY_MASK = 0xff,
FLAT_BINDER_FLAG_ACCEPTS_FDS = 0x100,
};
#ifdef BINDER_IPC_32BIT
typedef __u32 binder_size_t;
typedef __u32 binder_uintptr_t;
#else
typedef __u64 binder_size_t;
typedef __u64 binder_uintptr_t;
#endif
struct binder_object_header {
__u32 type;
};
struct flat_binder_object {
__u32 type;
__u32 flags;
union {
binder_uintptr_t binder;
__u32 handle;
};
binder_uintptr_t cookie;
};
struct binder_fd_object {
struct binder_object_header hdr;
__u32 pad_flags;
union {
binder_uintptr_t pad_binder;
__u32 fd;
};
binder_uintptr_t cookie;
};
struct binder_buffer_object {
struct binder_object_header hdr;
__u32 flags;
binder_uintptr_t buffer;
binder_size_t length;
binder_size_t parent;
binder_size_t parent_offset;
};
enum {
BINDER_BUFFER_FLAG_HAS_PARENT = 0x01,
};
struct binder_fd_array_object {
struct binder_object_header hdr;
__u32 pad;
binder_size_t num_fds;
binder_size_t parent;
binder_size_t parent_offset;
};
struct binder_write_read {
binder_size_t write_size;
binder_size_t write_consumed;
binder_uintptr_t write_buffer;
binder_size_t read_size;
binder_size_t read_consumed;
binder_uintptr_t read_buffer;
};
struct binder_version {
__s32 protocol_version;
};
#ifdef BINDER_IPC_32BIT
#define BINDER_CURRENT_PROTOCOL_VERSION 7
#else
#define BINDER_CURRENT_PROTOCOL_VERSION 8
#endif
struct binder_node_debug_info {
binder_uintptr_t ptr;
binder_uintptr_t cookie;
__u32 has_strong_ref;
__u32 has_weak_ref;
};
struct binder_ptr_count {
binder_uintptr_t ptr;
uint32_t count;
};
#define BINDER_WRITE_READ _IOWR('b', 1, struct binder_write_read)
#define BINDER_SET_IDLE_TIMEOUT _IOW('b', 3, __s64)
#define BINDER_SET_MAX_THREADS _IOW('b', 5, __u32)
#define BINDER_SET_IDLE_PRIORITY _IOW('b', 6, __s32)
#define BINDER_SET_CONTEXT_MGR _IOW('b', 7, __s32)
#define BINDER_THREAD_EXIT _IOW('b', 8, __s32)
#define BINDER_VERSION _IOWR('b', 9, struct binder_version)
#define BINDER_GET_NODE_DEBUG_INFO _IOWR('b', 11, struct binder_node_debug_info)
#define BINDER_GET_NODE_REFCOUNT _IOWR('b', 17, struct binder_ptr_count)
enum transaction_flags {
TF_ONE_WAY = 0x01,
TF_ROOT_OBJECT = 0x04,
TF_STATUS_CODE = 0x08,
TF_ACCEPT_FDS = 0x10,
};
struct binder_transaction_data {
union {
__u32 handle;
binder_uintptr_t ptr;
} target;
binder_uintptr_t cookie;
__u32 code;
__u32 flags;
pid_t sender_pid;
uid_t sender_euid;
binder_size_t data_size;
binder_size_t offsets_size;
union {
struct {
binder_uintptr_t buffer;
binder_uintptr_t offsets;
} ptr;
__u8 buf[8];
} data;
};
struct binder_transaction_data_sg {
struct binder_transaction_data transaction_data;
binder_size_t buffers_size;
};
struct binder_ptr_cookie {
binder_uintptr_t ptr;
binder_uintptr_t cookie;
};
struct binder_handle_cookie {
__u32 handle;
binder_uintptr_t cookie;
}__attribute__((__packed__));
struct binder_pri_desc {
__s32 priority;
__u32 desc;
};
struct binder_pri_ptr_cookie {
__s32 priority;
binder_uintptr_t ptr;
binder_uintptr_t cookie;
};
enum binder_driver_return_protocol {
BR_ERROR = _IOR('r', 0, __s32),
BR_OK = _IO('r', 1),
BR_TRANSACTION = _IOR('r', 2, struct binder_transaction_data),
BR_REPLY = _IOR('r', 3, struct binder_transaction_data),
BR_ACQUIRE_RESULT = _IOR('r', 4, __s32),
BR_DEAD_REPLY = _IO('r', 5),
BR_TRANSACTION_COMPLETE = _IO('r', 6),
BR_INCREFS = _IOR('r', 7, struct binder_ptr_cookie),
BR_ACQUIRE = _IOR('r', 8, struct binder_ptr_cookie),
BR_RELEASE = _IOR('r', 9, struct binder_ptr_cookie),
BR_DECREFS = _IOR('r', 10, struct binder_ptr_cookie),
BR_ATTEMPT_ACQUIRE = _IOR('r', 11, struct binder_pri_ptr_cookie),
BR_NOOP = _IO('r', 12),
BR_SPAWN_LOOPER = _IO('r', 13),
BR_FINISHED = _IO('r', 14),
BR_DEAD_BINDER = _IOR('r', 15, binder_uintptr_t),
BR_CLEAR_DEATH_NOTIFICATION_DONE = _IOR('r', 16, binder_uintptr_t),
BR_FAILED_REPLY = _IO('r', 17),
BR_RELEASE_NODE = _IO('r', 18),
};
enum binder_driver_command_protocol {
BC_TRANSACTION = _IOW('c', 0, struct binder_transaction_data),
BC_REPLY = _IOW('c', 1, struct binder_transaction_data),
BC_ACQUIRE_RESULT = _IOW('c', 2, __s32),
BC_FREE_BUFFER = _IOW('c', 3, binder_uintptr_t),
BC_INCREFS = _IOW('c', 4, __u32),
BC_ACQUIRE = _IOW('c', 5, __u32),
BC_RELEASE = _IOW('c', 6, __u32),
BC_DECREFS = _IOW('c', 7, __u32),
BC_INCREFS_DONE = _IOW('c', 8, struct binder_ptr_cookie),
BC_ACQUIRE_DONE = _IOW('c', 9, struct binder_ptr_cookie),
BC_ATTEMPT_ACQUIRE = _IOW('c', 10, struct binder_pri_desc),
BC_REGISTER_LOOPER = _IO('c', 11),
BC_ENTER_LOOPER = _IO('c', 12),
BC_EXIT_LOOPER = _IO('c', 13),
BC_REQUEST_DEATH_NOTIFICATION = _IOW('c', 14, struct binder_handle_cookie),
BC_CLEAR_DEATH_NOTIFICATION = _IOW('c', 15, struct binder_handle_cookie),
BC_DEAD_BINDER_DONE = _IOW('c', 16, binder_uintptr_t),
BC_TRANSACTION_SG = _IOW('c', 17, struct binder_transaction_data_sg),
BC_REPLY_SG = _IOW('c', 18, struct binder_transaction_data_sg),
BC_RELEASE_NODE = _IOW('c', 19, binder_uintptr_t),
};
#endif
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ipc_skeleton.h"
#include "ipc_process_skeleton.h"
#include "rpc_errno.h"
#include "rpc_log.h"
#include "rpc_types.h"
#include "securec.h"
#include "utils_list.h"
// default is 4 max is 16
int32_t SetMaxWorkThreadNum(int32_t maxThreadNum)
{
if (GetCurrentSkeleton() == NULL) {
RPC_LOG_ERROR("init ipc process skeleton failed.");
return ERR_IPC_SKELETON_NOT_INIT;
}
if ((maxThreadNum < SET_MAX_THREADS_DEFAULT) || (maxThreadNum > SET_MAX_THREADS_MAX)) {
RPC_LOG_ERROR("max thread num is invalid.");
return ERR_INVALID_PARAM;
}
return SetMaxWorkThread(maxThreadNum);
}
// join current thread into work loop.
void JoinWorkThread(void)
{
if (GetCurrentSkeleton() == NULL) {
RPC_LOG_ERROR("init ipc process skeleton failed.");
return;
}
return JoinMainWorkThread();
}
pid_t GetCallingPid(void)
{
return ProcessGetCallingPid();
}
pid_t GetCallingUid(void)
{
return ProcessGetCallingUid();
}
const SvcIdentity *GetContextObject(void)
{
if (GetCurrentSkeleton() == NULL) {
RPC_LOG_ERROR("init ipc process skeleton failed.");
return NULL;
}
return GetRegistryObject();
}
int32_t SetContextObject(SvcIdentity target)
{
if (GetCurrentSkeleton() == NULL) {
RPC_LOG_ERROR("init ipc process skeleton failed.");
return ERR_IPC_SKELETON_NOT_INIT;
}
if (target.cookie == 0) {
RPC_LOG_ERROR("samgr stub func is NULL.");
return ERR_INVALID_PARAM;
}
return SetRegistryObject(target);
}
int32_t SendRequest(SvcIdentity target, uint32_t code, IpcIo *data, IpcIo *reply,
MessageOption option, uintptr_t *buffer)
{
if (GetCurrentSkeleton() == NULL) {
RPC_LOG_ERROR("init ipc process skeleton failed.");
return ERR_IPC_SKELETON_NOT_INIT;
}
return ProcessSendRequest(target, code, data, reply, option, buffer);
}
int32_t AddDeathRecipient(SvcIdentity target, OnRemoteDead deathFunc, void *args, uint32_t *cbId)
{
if (GetCurrentSkeleton() == NULL) {
RPC_LOG_ERROR("init ipc process skeleton failed.");
return ERR_IPC_SKELETON_NOT_INIT;
}
if (target.handle < 0) {
RPC_LOG_ERROR("add death recipient is invalid handle.");
return ERR_INVALID_PARAM;
}
return ProcessAddDeathRecipient(target.handle, deathFunc, args, cbId);
}
int32_t RemoveDeathRecipient(SvcIdentity target, uint32_t cbId)
{
if (GetCurrentSkeleton() == NULL) {
RPC_LOG_ERROR("init ipc process skeleton failed.");
return ERR_IPC_SKELETON_NOT_INIT;
}
if (target.handle < 0) {
RPC_LOG_ERROR("add death recipient is invalid handle.");
return ERR_INVALID_PARAM;
}
return ProcessRemoveDeathRecipient(target.handle, cbId);
}
int32_t FreeBuffer(void *ptr)
{
if (ptr == NULL) {
RPC_LOG_ERROR("ptr is null, no data to free");
return ERR_INVALID_PARAM;
}
if (GetCurrentSkeleton() == NULL) {
RPC_LOG_ERROR("init ipc process skeleton failed.");
return ERR_IPC_SKELETON_NOT_INIT;
}
return ProcessFreeBuffer(ptr);
}
int32_t MessageOptionInit(MessageOption *option)
{
if (option == NULL) {
RPC_LOG_ERROR("option is null");
return ERR_INVALID_PARAM;
}
option->flags = TF_OP_SYNC;
option->waitTime = RPC_DEFAULT_SEND_WAIT_TIME;
option->args = NULL;
return ERR_NONE;
}
int32_t ReleaseSvc(SvcIdentity target)
{
if (GetCurrentSkeleton() == NULL) {
RPC_LOG_ERROR("init ipc process skeleton failed.");
return ERR_IPC_SKELETON_NOT_INIT;
}
if (target.handle <= 0) {
RPC_LOG_ERROR("release svc is invalid handle.");
return ERR_INVALID_PARAM;
}
return DeleteHandle(target.handle);
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ipc_skeleton.h"
#include "ipc_process_skeleton.h"
#include "rpc_errno.h"
#include "rpc_log.h"
#include "rpc_types.h"
#include "securec.h"
#include "utils_list.h"
// default is 4 max is 16
int32_t SetMaxWorkThreadNum(int32_t maxThreadNum)
{
if (GetCurrentSkeleton() == NULL) {
RPC_LOG_ERROR("init ipc process skeleton failed.");
return ERR_IPC_SKELETON_NOT_INIT;
}
if ((maxThreadNum < SET_MAX_THREADS_DEFAULT) || (maxThreadNum > SET_MAX_THREADS_MAX)) {
RPC_LOG_ERROR("max thread num is invalid.");
return ERR_INVALID_PARAM;
}
return SetMaxWorkThread(maxThreadNum);
}
// join current thread into work loop.
void JoinWorkThread(void)
{
if (GetCurrentSkeleton() == NULL) {
RPC_LOG_ERROR("init ipc process skeleton failed.");
return;
}
return JoinMainWorkThread();
}
pid_t GetCallingPid(void)
{
return ProcessGetCallingPid();
}
pid_t GetCallingUid(void)
{
return ProcessGetCallingUid();
}
const SvcIdentity *GetContextObject(void)
{
if (GetCurrentSkeleton() == NULL) {
RPC_LOG_ERROR("init ipc process skeleton failed.");
return NULL;
}
return GetRegistryObject();
}
int32_t SetContextObject(SvcIdentity target)
{
if (GetCurrentSkeleton() == NULL) {
RPC_LOG_ERROR("init ipc process skeleton failed.");
return ERR_IPC_SKELETON_NOT_INIT;
}
if (target.cookie == 0) {
RPC_LOG_ERROR("samgr stub func is NULL.");
return ERR_INVALID_PARAM;
}
return SetRegistryObject(target);
}
int32_t SendRequest(SvcIdentity target, uint32_t code, IpcIo *data, IpcIo *reply,
MessageOption option, uintptr_t *buffer)
{
if (GetCurrentSkeleton() == NULL) {
RPC_LOG_ERROR("init ipc process skeleton failed.");
return ERR_IPC_SKELETON_NOT_INIT;
}
return ProcessSendRequest(target, code, data, reply, option, buffer);
}
int32_t AddDeathRecipient(SvcIdentity target, OnRemoteDead deathFunc, void *args, uint32_t *cbId)
{
if (GetCurrentSkeleton() == NULL) {
RPC_LOG_ERROR("init ipc process skeleton failed.");
return ERR_IPC_SKELETON_NOT_INIT;
}
if (target.handle < 0) {
RPC_LOG_ERROR("add death recipient is invalid handle.");
return ERR_INVALID_PARAM;
}
return ProcessAddDeathRecipient(target.handle, deathFunc, args, cbId);
}
int32_t RemoveDeathRecipient(SvcIdentity target, uint32_t cbId)
{
if (GetCurrentSkeleton() == NULL) {
RPC_LOG_ERROR("init ipc process skeleton failed.");
return ERR_IPC_SKELETON_NOT_INIT;
}
if (target.handle < 0) {
RPC_LOG_ERROR("add death recipient is invalid handle.");
return ERR_INVALID_PARAM;
}
return ProcessRemoveDeathRecipient(target.handle, cbId);
}
int32_t FreeBuffer(void *ptr)
{
if (ptr == NULL) {
RPC_LOG_ERROR("ptr is null, no data to free");
return ERR_INVALID_PARAM;
}
if (GetCurrentSkeleton() == NULL) {
RPC_LOG_ERROR("init ipc process skeleton failed.");
return ERR_IPC_SKELETON_NOT_INIT;
}
return ProcessFreeBuffer(ptr);
}
int32_t MessageOptionInit(MessageOption *option)
{
if (option == NULL) {
RPC_LOG_ERROR("option is null");
return ERR_INVALID_PARAM;
}
option->flags = TF_OP_SYNC;
option->waitTime = RPC_DEFAULT_SEND_WAIT_TIME;
option->args = NULL;
return ERR_NONE;
}
int32_t ReleaseSvc(SvcIdentity target)
{
if (GetCurrentSkeleton() == NULL) {
RPC_LOG_ERROR("init ipc process skeleton failed.");
return ERR_IPC_SKELETON_NOT_INIT;
}
if (target.handle <= 0) {
RPC_LOG_ERROR("release svc is invalid handle.");
return ERR_INVALID_PARAM;
}
return DeleteHandle(target.handle);
}
\ No newline at end of file
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ipc_thread_pool.h"
#include <unistd.h>
#include "ipc_process_skeleton.h"
#include "iremote_invoker.h"
#include "rpc_errno.h"
#include "rpc_log.h"
#include "rpc_os_adapter.h"
#include "rpc_types.h"
#include "securec.h"
#define PROTO_NUM 2
static pthread_key_t g_localKey = -1;
static RemoteInvoker *g_invoker[PROTO_NUM];
ThreadContext *GetCurrentThreadContext(void)
{
ThreadContext *current = NULL;
void *curTLS = pthread_getspecific(g_localKey);
if (curTLS != NULL) {
current = (ThreadContext *)curTLS;
} else {
current = (ThreadContext *)calloc(1, sizeof(ThreadContext));
if (current == NULL) {
return NULL;
}
current->threadId = pthread_self();
current->proto = IF_PROT_DEFAULT;
current->callerPid = RpcGetPid();
current->callerUid = RpcGetUid();
pthread_setspecific(g_localKey, current);
}
return current;
}
static void TlsDestructor(void *args)
{
ThreadContext *threadContext = (ThreadContext *)args;
RemoteInvoker *invoker = g_invoker[threadContext->proto];
free(threadContext);
if (invoker != NULL && invoker->ExitCurrentThread != NULL) {
(invoker->ExitCurrentThread)();
}
}
static void ThreadContextDestructor(int32_t proto)
{
ThreadPool *threadPool = GetCurrentSkeleton()->threadPool;
pthread_mutex_lock(&threadPool->lock);
if (proto == IF_PROT_BINDER) {
++threadPool->idleThreadNum;
} else if (proto == IF_PROT_DATABUS) {
++threadPool->idleSocketThreadNum;
}
pthread_mutex_unlock(&threadPool->lock);
}
static RemoteInvoker *GetAndUpdateInvoker(int32_t proto)
{
ThreadContext *threadContext = GetCurrentThreadContext();
if (threadContext == NULL) {
return NULL;
}
threadContext->proto = proto;
return g_invoker[proto];
}
static void *ThreadHandler(void *args)
{
ThreadContext *threadContext = (ThreadContext *)args;
int32_t proto = threadContext->proto;
int32_t policy = threadContext->policy;
free(threadContext);
threadContext = NULL;
RemoteInvoker *invoker = GetAndUpdateInvoker(proto);
if (invoker != NULL) {
switch (policy) {
case SPAWN_PASSIVE:
invoker->JoinThread(false);
break;
case SPAWN_ACTIVE:
invoker->JoinThread(true);
break;
default:
break;
}
}
ThreadContextDestructor(proto);
return NULL;
}
ThreadPool *InitThreadPool(int32_t maxThreadNum)
{
ThreadPool *threadPool = (ThreadPool*)calloc(1, sizeof(ThreadPool));
if (threadPool == NULL) {
return NULL;
}
threadPool->maxThreadNum = maxThreadNum + maxThreadNum;
threadPool->idleThreadNum = maxThreadNum;
threadPool->idleSocketThreadNum = maxThreadNum;
pthread_mutex_init(&threadPool->lock, NULL);
pthread_key_create(&g_localKey, TlsDestructor);
for (int32_t index = 0; index < PROTO_NUM; ++index) {
g_invoker[index] = InitRemoteInvoker(index);
}
return threadPool;
}
void DeinitThreadPool(ThreadPool *threadPool)
{
if (threadPool == NULL) {
return;
}
pthread_mutex_destroy(&threadPool->lock);
pthread_key_delete(g_localKey);
free(threadPool);
for (int32_t index = 0; index < PROTO_NUM; ++index) {
DeinitRemoteInvoker(g_invoker[index], index);
g_invoker[index] = NULL;
}
}
int32_t SpawnNewThread(ThreadPool *threadPool, int32_t policy, int32_t proto)
{
if (!(proto == IF_PROT_BINDER && threadPool->idleThreadNum > 0) &&
!(proto == IF_PROT_DATABUS && threadPool->idleSocketThreadNum > 0)) {
RPC_LOG_ERROR("thread pool is full.");
return ERR_INVALID_PARAM;
}
pthread_t threadId;
if (pthread_mutex_lock(&threadPool->lock) != 0) {
RPC_LOG_ERROR("get thread pool lock failed.");
return ERR_FAILED;
}
if (!(proto == IF_PROT_BINDER && threadPool->idleThreadNum > 0) &&
!(proto == IF_PROT_DATABUS && threadPool->idleSocketThreadNum > 0)) {
pthread_mutex_unlock(&threadPool->lock);
RPC_LOG_ERROR("thread pool is full.");
return ERR_INVALID_PARAM;
}
ThreadContext *threadContext = (ThreadContext *)calloc(1, sizeof(ThreadContext));
if (threadContext == NULL) {
pthread_mutex_unlock(&threadPool->lock);
RPC_LOG_ERROR("create thread context failed.");
return ERR_FAILED;
}
threadContext->proto = proto;
threadContext->policy = policy;
int ret = pthread_create(&threadId, NULL, ThreadHandler, threadContext);
if (ret != 0) {
pthread_mutex_unlock(&threadPool->lock);
free(threadContext);
RPC_LOG_ERROR("spawn new thread failed.");
return ERR_FAILED;
}
pthread_detach(threadId);
if (proto == IF_PROT_BINDER) {
--threadPool->idleThreadNum;
} else if (proto == IF_PROT_DATABUS) {
--threadPool->idleSocketThreadNum;
}
pthread_mutex_unlock(&threadPool->lock);
return ERR_NONE;
}
void UpdateMaxThreadNum(ThreadPool *threadPool, int32_t maxThreadNum)
{
int32_t totalNum = maxThreadNum + maxThreadNum;
if (pthread_mutex_lock(&threadPool->lock) != 0) {
RPC_LOG_ERROR("get thread pool lock failed.");
return;
}
int32_t oldThreadNum = threadPool->maxThreadNum;
if (totalNum <= oldThreadNum) {
pthread_mutex_unlock(&threadPool->lock);
RPC_LOG_ERROR("not support set lower max thread num.");
return;
}
int32_t diff = totalNum - oldThreadNum;
threadPool->maxThreadNum = totalNum;
threadPool->idleThreadNum += diff / PROTO_NUM;
threadPool->idleSocketThreadNum += diff / PROTO_NUM;
pthread_mutex_unlock(&threadPool->lock);
}
RemoteInvoker *GetRemoteInvoker(void)
{
ThreadContext *threadContext = GetCurrentThreadContext();
if (threadContext == NULL) {
return NULL;
}
return g_invoker[threadContext->proto];
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ipc_thread_pool.h"
#include <unistd.h>
#include "ipc_process_skeleton.h"
#include "iremote_invoker.h"
#include "rpc_errno.h"
#include "rpc_log.h"
#include "rpc_os_adapter.h"
#include "rpc_types.h"
#include "securec.h"
#define PROTO_NUM 2
static pthread_key_t g_localKey = -1;
static RemoteInvoker *g_invoker[PROTO_NUM];
ThreadContext *GetCurrentThreadContext(void)
{
ThreadContext *current = NULL;
void *curTLS = pthread_getspecific(g_localKey);
if (curTLS != NULL) {
current = (ThreadContext *)curTLS;
} else {
current = (ThreadContext *)calloc(1, sizeof(ThreadContext));
if (current == NULL) {
return NULL;
}
current->threadId = pthread_self();
current->proto = IF_PROT_DEFAULT;
current->callerPid = RpcGetPid();
current->callerUid = RpcGetUid();
pthread_setspecific(g_localKey, current);
}
return current;
}
static void TlsDestructor(void *args)
{
ThreadContext *threadContext = (ThreadContext *)args;
RemoteInvoker *invoker = g_invoker[threadContext->proto];
free(threadContext);
if (invoker != NULL && invoker->ExitCurrentThread != NULL) {
(invoker->ExitCurrentThread)();
}
}
static void ThreadContextDestructor(int32_t proto)
{
ThreadPool *threadPool = GetCurrentSkeleton()->threadPool;
pthread_mutex_lock(&threadPool->lock);
if (proto == IF_PROT_BINDER) {
++threadPool->idleThreadNum;
} else if (proto == IF_PROT_DATABUS) {
++threadPool->idleSocketThreadNum;
}
pthread_mutex_unlock(&threadPool->lock);
}
static RemoteInvoker *GetAndUpdateInvoker(int32_t proto)
{
ThreadContext *threadContext = GetCurrentThreadContext();
if (threadContext == NULL) {
return NULL;
}
threadContext->proto = proto;
return g_invoker[proto];
}
static void *ThreadHandler(void *args)
{
ThreadContext *threadContext = (ThreadContext *)args;
int32_t proto = threadContext->proto;
int32_t policy = threadContext->policy;
free(threadContext);
threadContext = NULL;
RemoteInvoker *invoker = GetAndUpdateInvoker(proto);
if (invoker != NULL) {
switch (policy) {
case SPAWN_PASSIVE:
invoker->JoinThread(false);
break;
case SPAWN_ACTIVE:
invoker->JoinThread(true);
break;
default:
break;
}
}
ThreadContextDestructor(proto);
return NULL;
}
ThreadPool *InitThreadPool(int32_t maxThreadNum)
{
ThreadPool *threadPool = (ThreadPool*)calloc(1, sizeof(ThreadPool));
if (threadPool == NULL) {
return NULL;
}
threadPool->maxThreadNum = maxThreadNum + maxThreadNum;
threadPool->idleThreadNum = maxThreadNum;
threadPool->idleSocketThreadNum = maxThreadNum;
pthread_mutex_init(&threadPool->lock, NULL);
pthread_key_create(&g_localKey, TlsDestructor);
for (int32_t index = 0; index < PROTO_NUM; ++index) {
g_invoker[index] = InitRemoteInvoker(index);
}
return threadPool;
}
void DeinitThreadPool(ThreadPool *threadPool)
{
if (threadPool == NULL) {
return;
}
pthread_mutex_destroy(&threadPool->lock);
pthread_key_delete(g_localKey);
free(threadPool);
for (int32_t index = 0; index < PROTO_NUM; ++index) {
DeinitRemoteInvoker(g_invoker[index], index);
g_invoker[index] = NULL;
}
}
int32_t SpawnNewThread(ThreadPool *threadPool, int32_t policy, int32_t proto)
{
if (!(proto == IF_PROT_BINDER && threadPool->idleThreadNum > 0) &&
!(proto == IF_PROT_DATABUS && threadPool->idleSocketThreadNum > 0)) {
RPC_LOG_ERROR("thread pool is full.");
return ERR_INVALID_PARAM;
}
pthread_t threadId;
if (pthread_mutex_lock(&threadPool->lock) != 0) {
RPC_LOG_ERROR("get thread pool lock failed.");
return ERR_FAILED;
}
if (!(proto == IF_PROT_BINDER && threadPool->idleThreadNum > 0) &&
!(proto == IF_PROT_DATABUS && threadPool->idleSocketThreadNum > 0)) {
pthread_mutex_unlock(&threadPool->lock);
RPC_LOG_ERROR("thread pool is full.");
return ERR_INVALID_PARAM;
}
ThreadContext *threadContext = (ThreadContext *)calloc(1, sizeof(ThreadContext));
if (threadContext == NULL) {
pthread_mutex_unlock(&threadPool->lock);
RPC_LOG_ERROR("create thread context failed.");
return ERR_FAILED;
}
threadContext->proto = proto;
threadContext->policy = policy;
int ret = pthread_create(&threadId, NULL, ThreadHandler, threadContext);
if (ret != 0) {
pthread_mutex_unlock(&threadPool->lock);
free(threadContext);
RPC_LOG_ERROR("spawn new thread failed.");
return ERR_FAILED;
}
pthread_detach(threadId);
if (proto == IF_PROT_BINDER) {
--threadPool->idleThreadNum;
} else if (proto == IF_PROT_DATABUS) {
--threadPool->idleSocketThreadNum;
}
pthread_mutex_unlock(&threadPool->lock);
return ERR_NONE;
}
void UpdateMaxThreadNum(ThreadPool *threadPool, int32_t maxThreadNum)
{
int32_t totalNum = maxThreadNum + maxThreadNum;
if (pthread_mutex_lock(&threadPool->lock) != 0) {
RPC_LOG_ERROR("get thread pool lock failed.");
return;
}
int32_t oldThreadNum = threadPool->maxThreadNum;
if (totalNum <= oldThreadNum) {
pthread_mutex_unlock(&threadPool->lock);
RPC_LOG_ERROR("not support set lower max thread num.");
return;
}
int32_t diff = totalNum - oldThreadNum;
threadPool->maxThreadNum = totalNum;
threadPool->idleThreadNum += diff / PROTO_NUM;
threadPool->idleSocketThreadNum += diff / PROTO_NUM;
pthread_mutex_unlock(&threadPool->lock);
}
RemoteInvoker *GetRemoteInvoker(void)
{
ThreadContext *threadContext = GetCurrentThreadContext();
if (threadContext == NULL) {
return NULL;
}
return g_invoker[threadContext->proto];
}
\ No newline at end of file
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "iremote_invoker.h"
#include "dbinder_invoker.h"
#include "ipc_invoker.h"
#include "rpc_types.h"
RemoteInvoker *InitRemoteInvoker(int32_t proto)
{
RemoteInvoker *remoteInvoker = NULL;
if (proto == IF_PROT_BINDER) {
remoteInvoker = GetIpcInvoker();
} else {
remoteInvoker = GetRpcInvoker();
}
return remoteInvoker;
}
void DeinitRemoteInvoker(RemoteInvoker *invoker, int32_t proto)
{
if (invoker == NULL) {
return;
}
if (proto == IF_PROT_BINDER) {
DeinitIpcInvoker(invoker);
}
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "iremote_invoker.h"
#include "dbinder_invoker.h"
#include "ipc_invoker.h"
#include "rpc_types.h"
RemoteInvoker *InitRemoteInvoker(int32_t proto)
{
RemoteInvoker *remoteInvoker = NULL;
if (proto == IF_PROT_BINDER) {
remoteInvoker = GetIpcInvoker();
} else {
remoteInvoker = GetRpcInvoker();
}
return remoteInvoker;
}
void DeinitRemoteInvoker(RemoteInvoker *invoker, int32_t proto)
{
if (invoker == NULL) {
return;
}
if (proto == IF_PROT_BINDER) {
DeinitIpcInvoker(invoker);
}
}
\ No newline at end of file
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "dbinder_callback_stub.h"
#include <cinttypes>
#include "log_tags.h"
#include "ipc_debug.h"
#include "ipc_process_skeleton.h"
#include "ipc_skeleton.h"
#include "ipc_thread_skeleton.h"
#include "ipc_types.h"
#include "string_ex.h"
#include "sys_binder.h"
namespace OHOS {
static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_RPC, "DBinderCallbackStub" };
DBinderCallbackStub::DBinderCallbackStub(const std::string &service, const std::string &device,
const std::string &localDevice, uint64_t stubIndex, uint32_t handle, uint32_t tokenId)
: IPCObjectStub(Str8ToStr16("DBinderCallback" + device + service)),
serviceName_(service),
deviceID_(device),
localDeviceID_(localDevice),
stubIndex_(stubIndex),
handle_(handle),
tokenId_(tokenId)
{
ZLOGI(LOG_LABEL, "serviceName:%{public}s, deviceId:%{public}s, handle:%{public}u, stubIndex_:%{public}" PRIu64 "",
serviceName_.c_str(), deviceID_.c_str(), handle_, stubIndex_);
}
DBinderCallbackStub::~DBinderCallbackStub()
{
ZLOGI(LOG_LABEL, "DBinderCallbackStub delete");
IPCProcessSkeleton::GetCurrent()->DetachDBinderCallbackStub(this);
}
const std::string &DBinderCallbackStub::GetServiceName()
{
return serviceName_;
}
const std::string &DBinderCallbackStub::GetDeviceID()
{
return deviceID_;
}
uint64_t DBinderCallbackStub::GetStubIndex() const
{
return stubIndex_;
}
uint32_t DBinderCallbackStub::GetTokenId() const
{
return tokenId_;
}
int32_t DBinderCallbackStub::ProcessProto(uint32_t code, MessageParcel &data, MessageParcel &reply,
MessageOption &option)
{
int uid = IPCSkeleton::GetCallingUid();
int pid = IPCSkeleton::GetCallingPid();
if (uid < 0 || pid < 0) {
ZLOGE(LOG_LABEL, "uid or pid err");
return DBINDER_SERVICE_PROCESS_PROTO_ERR;
}
sptr<IRemoteObject> object = IPCProcessSkeleton::GetCurrent()->GetSAMgrObject();
if (object == nullptr) {
ZLOGE(LOG_LABEL, "get sa object is null");
return DBINDER_CALLBACK_READ_OBJECT_ERR;
}
IPCObjectProxy *samgr = reinterpret_cast<IPCObjectProxy *>(object.GetRefPtr());
std::string sessionName = samgr->GetSessionNameForPidUid(uid, pid);
if (sessionName.empty()) {
ZLOGE(LOG_LABEL, "grans session name failed");
return DBINDER_SERVICE_WRONG_SESSION;
}
MessageParcel authData, authReply;
MessageOption authOption;
if (!authData.WriteUint32(pid) || !authData.WriteUint32(uid) || !authData.WriteString(localDeviceID_) ||
!authData.WriteUint64(stubIndex_) || !authData.WriteUint32(tokenId_)) {
ZLOGE(LOG_LABEL, "write to MessageParcel fail");
return ERR_INVALID_DATA;
}
IRemoteInvoker *dbinderInvoker = IPCThreadSkeleton::GetRemoteInvoker(IRemoteObject::IF_PROT_DATABUS);
if (dbinderInvoker == nullptr) {
ZLOGE(LOG_LABEL, "no databus thread and invoker");
return RPC_DATABUS_INVOKER_ERR;
}
int err = dbinderInvoker->SendRequest(handle_, DBINDER_ADD_COMMAUTH, authData, authReply, authOption);
if (err != ERR_NONE) {
ZLOGE(LOG_LABEL, "send auth info to remote fail");
return BINDER_CALLBACK_AUTHCOMM_ERR;
}
ZLOGI(LOG_LABEL, "send to stub ok! stubIndex:%{public}" PRIu64 ", peerDevice = %{public}s, "
"localDeviceID_ = %{public}s, serviceName_ = %{public}s, uid:%{public}d, pid:%{public}d, "
"tokenId: %{public}u, sessionName = %{public}s",
stubIndex_, IPCProcessSkeleton::ConvertToSecureString(deviceID_).c_str(),
IPCProcessSkeleton::ConvertToSecureString(localDeviceID_).c_str(), serviceName_.c_str(), uid, pid, tokenId_,
sessionName.c_str());
if (!reply.WriteUint32(IRemoteObject::IF_PROT_DATABUS) || !reply.WriteUint64(stubIndex_) ||
!reply.WriteString(serviceName_) || !reply.WriteString(deviceID_) || !reply.WriteString(localDeviceID_) ||
!reply.WriteString(sessionName) || !reply.WriteUint32(tokenId_)) {
ZLOGE(LOG_LABEL, "write to parcel fail");
return ERR_INVALID_DATA;
}
return 0;
}
int32_t DBinderCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
MessageOption &option)
{
int32_t result = ERR_NONE;
ZLOGI(LOG_LABEL, "code = %{public}u", code);
switch (code) {
case GET_PROTO_INFO: {
result = ProcessProto(code, data, reply, option);
break;
}
default: {
ZLOGI(LOG_LABEL, "unknown code = %{public}u", code);
result = DBINDER_CALLBACK_ERR;
break;
}
}
return result;
}
} // namespace OHOS
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "dbinder_callback_stub.h"
#include <cinttypes>
#include "log_tags.h"
#include "ipc_debug.h"
#include "ipc_process_skeleton.h"
#include "ipc_skeleton.h"
#include "ipc_thread_skeleton.h"
#include "ipc_types.h"
#include "string_ex.h"
#include "sys_binder.h"
namespace OHOS {
static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_RPC, "DBinderCallbackStub" };
DBinderCallbackStub::DBinderCallbackStub(const std::string &service, const std::string &device,
const std::string &localDevice, uint64_t stubIndex, uint32_t handle, uint32_t tokenId)
: IPCObjectStub(Str8ToStr16("DBinderCallback" + device + service)),
serviceName_(service),
deviceID_(device),
localDeviceID_(localDevice),
stubIndex_(stubIndex),
handle_(handle),
tokenId_(tokenId)
{
ZLOGI(LOG_LABEL, "serviceName:%{public}s, deviceId:%{public}s, handle:%{public}u, stubIndex_:%{public}" PRIu64 "",
serviceName_.c_str(), deviceID_.c_str(), handle_, stubIndex_);
}
DBinderCallbackStub::~DBinderCallbackStub()
{
ZLOGI(LOG_LABEL, "DBinderCallbackStub delete");
IPCProcessSkeleton::GetCurrent()->DetachDBinderCallbackStub(this);
}
const std::string &DBinderCallbackStub::GetServiceName()
{
return serviceName_;
}
const std::string &DBinderCallbackStub::GetDeviceID()
{
return deviceID_;
}
uint64_t DBinderCallbackStub::GetStubIndex() const
{
return stubIndex_;
}
uint32_t DBinderCallbackStub::GetTokenId() const
{
return tokenId_;
}
int32_t DBinderCallbackStub::ProcessProto(uint32_t code, MessageParcel &data, MessageParcel &reply,
MessageOption &option)
{
int uid = IPCSkeleton::GetCallingUid();
int pid = IPCSkeleton::GetCallingPid();
if (uid < 0 || pid < 0) {
ZLOGE(LOG_LABEL, "uid or pid err");
return DBINDER_SERVICE_PROCESS_PROTO_ERR;
}
sptr<IRemoteObject> object = IPCProcessSkeleton::GetCurrent()->GetSAMgrObject();
if (object == nullptr) {
ZLOGE(LOG_LABEL, "get sa object is null");
return DBINDER_CALLBACK_READ_OBJECT_ERR;
}
IPCObjectProxy *samgr = reinterpret_cast<IPCObjectProxy *>(object.GetRefPtr());
std::string sessionName = samgr->GetSessionNameForPidUid(uid, pid);
if (sessionName.empty()) {
ZLOGE(LOG_LABEL, "grans session name failed");
return DBINDER_SERVICE_WRONG_SESSION;
}
MessageParcel authData, authReply;
MessageOption authOption;
if (!authData.WriteUint32(pid) || !authData.WriteUint32(uid) || !authData.WriteString(localDeviceID_) ||
!authData.WriteUint64(stubIndex_) || !authData.WriteUint32(tokenId_)) {
ZLOGE(LOG_LABEL, "write to MessageParcel fail");
return ERR_INVALID_DATA;
}
IRemoteInvoker *dbinderInvoker = IPCThreadSkeleton::GetRemoteInvoker(IRemoteObject::IF_PROT_DATABUS);
if (dbinderInvoker == nullptr) {
ZLOGE(LOG_LABEL, "no databus thread and invoker");
return RPC_DATABUS_INVOKER_ERR;
}
int err = dbinderInvoker->SendRequest(handle_, DBINDER_ADD_COMMAUTH, authData, authReply, authOption);
if (err != ERR_NONE) {
ZLOGE(LOG_LABEL, "send auth info to remote fail");
return BINDER_CALLBACK_AUTHCOMM_ERR;
}
ZLOGI(LOG_LABEL, "send to stub ok! stubIndex:%{public}" PRIu64 ", peerDevice = %{public}s, "
"localDeviceID_ = %{public}s, serviceName_ = %{public}s, uid:%{public}d, pid:%{public}d, "
"tokenId: %{public}u, sessionName = %{public}s",
stubIndex_, IPCProcessSkeleton::ConvertToSecureString(deviceID_).c_str(),
IPCProcessSkeleton::ConvertToSecureString(localDeviceID_).c_str(), serviceName_.c_str(), uid, pid, tokenId_,
sessionName.c_str());
if (!reply.WriteUint32(IRemoteObject::IF_PROT_DATABUS) || !reply.WriteUint64(stubIndex_) ||
!reply.WriteString(serviceName_) || !reply.WriteString(deviceID_) || !reply.WriteString(localDeviceID_) ||
!reply.WriteString(sessionName) || !reply.WriteUint32(tokenId_)) {
ZLOGE(LOG_LABEL, "write to parcel fail");
return ERR_INVALID_DATA;
}
return 0;
}
int32_t DBinderCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
MessageOption &option)
{
int32_t result = ERR_NONE;
ZLOGI(LOG_LABEL, "code = %{public}u", code);
switch (code) {
case GET_PROTO_INFO: {
result = ProcessProto(code, data, reply, option);
break;
}
default: {
ZLOGI(LOG_LABEL, "unknown code = %{public}u", code);
result = DBINDER_CALLBACK_ERR;
break;
}
}
return result;
}
} // namespace OHOS
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册