From 822148305cb03a42e8cc5883f1c0d05502ab0a11 Mon Sep 17 00:00:00 2001 From: SoulPoet <704936510@qq.com> Date: Tue, 29 Mar 2022 17:33:42 +0800 Subject: [PATCH] replace ipc module --- dsoftbus/depend/BUILD.gn | 21 ++- dsoftbus/depend/ipc/include/depend.h | 118 ------------ dsoftbus/depend/ipc/include/ipc_base.h | 56 ++++++ dsoftbus/depend/ipc/include/ipc_center.h | 24 +++ dsoftbus/depend/ipc/include/ipc_object_stub.h | 17 ++ dsoftbus/depend/ipc/include/ipc_skeleton.h | 35 +++- dsoftbus/depend/ipc/include/ipc_types.h | 2 +- dsoftbus/depend/ipc/include/iremote_broker.h | 16 +- dsoftbus/depend/ipc/include/iremote_object.h | 43 ++++- dsoftbus/depend/ipc/include/iremote_parcel.h | 2 +- dsoftbus/depend/ipc/include/iremote_proxy.h | 43 ++++- dsoftbus/depend/ipc/include/iremote_stub.h | 28 ++- dsoftbus/depend/ipc/include/message_option.h | 10 + dsoftbus/depend/ipc/include/message_parcel.h | 29 ++- dsoftbus/depend/ipc/include/system_ability.h | 45 ++++- .../ipc/include/system_ability_definition.h | 16 +- dsoftbus/depend/ipc/ipc_center.cpp | 99 ++++++++++ dsoftbus/depend/ipc/ipc_object_stub.cpp | 16 ++ dsoftbus/depend/ipc/ipc_skeleton.cpp | 174 ++++++++++++++++++ dsoftbus/depend/ipc/iremote_object.cpp | 70 +++++++ dsoftbus/depend/ipc/message_parcel.cpp | 73 ++++++++ 21 files changed, 807 insertions(+), 130 deletions(-) delete mode 100644 dsoftbus/depend/ipc/include/depend.h create mode 100644 dsoftbus/depend/ipc/include/ipc_base.h create mode 100644 dsoftbus/depend/ipc/include/ipc_center.h create mode 100644 dsoftbus/depend/ipc/include/ipc_object_stub.h create mode 100644 dsoftbus/depend/ipc/include/message_option.h create mode 100644 dsoftbus/depend/ipc/ipc_center.cpp create mode 100644 dsoftbus/depend/ipc/ipc_object_stub.cpp create mode 100644 dsoftbus/depend/ipc/ipc_skeleton.cpp create mode 100644 dsoftbus/depend/ipc/iremote_object.cpp create mode 100644 dsoftbus/depend/ipc/message_parcel.cpp diff --git a/dsoftbus/depend/BUILD.gn b/dsoftbus/depend/BUILD.gn index 613a3c33..ef1d939f 100644 --- a/dsoftbus/depend/BUILD.gn +++ b/dsoftbus/depend/BUILD.gn @@ -21,11 +21,28 @@ config("ipc_config") { include_dirs = [ "ipc/include", "//utils/native/base/include" ] } -ohos_shared_library("ipc_single") { +group("ipc_single") { + deps = [ "//depend:ipc_core" ] public_configs = [ ":ipc_config" ] } -group("ipc_core") { +ohos_shared_library("ipc_core") { + include_dirs = [ + "ipc/include", + ] + + sources = [ + "ipc/ipc_center.cpp", + "ipc/ipc_object_stub.cpp", + "ipc/ipc_skeleton.cpp", + "ipc/iremote_object.cpp", + "ipc/message_parcel.cpp", + ] + + deps = [ + "//utils/native/base:utils", + ] + public_configs = [ ":ipc_config" ] } diff --git a/dsoftbus/depend/ipc/include/depend.h b/dsoftbus/depend/ipc/include/depend.h deleted file mode 100644 index 50cf6c78..00000000 --- a/dsoftbus/depend/ipc/include/depend.h +++ /dev/null @@ -1,118 +0,0 @@ -#ifndef _DEPEND_CPP_H -#define _DEPEND_CPP_H - -//IRemoteBroker -#ifdef __cplusplus -#include -#include -#include -#include -#include "parcel.h" - -typedef enum TypePermissionState { - PERMISSION_NOT_GRANTED = -1, - PERMISSION_GRANTED = 0, -} PermissionState; - -#define DECLARE_SYSTEM_ABILITY(className) \ - public: \ - virtual std::string GetClassName() override { \ - return #className; \ - } - -#define DECLARE_BASE_SYSTEM_ABILITY(className) \ - public: \ - virtual std::string GetClassName() = 0; - -namespace OHOS { -namespace Security { -namespace Permission { -}; -}; - -#define REGISTER_SYSTEM_ABILITY_BY_ID(abilityClassName, systemAbilityId, runOnCreate) \ - const bool abilityClassName##_##RegisterResult = \ - SystemAbility::MakeAndRegisterAbility(new abilityClassName(systemAbilityId, runOnCreate)); - -enum { - SOFTBUS_SERVER_SA_ID = 4700, -}; - -template class BrokerDelegator { -}; - -class IRemoteObject; - -class MessageParcel : public Parcel { -public: - bool WriteFileDescriptor(int fd) {return true;} - bool WriteRawData(const void *data, size_t size) {return true;} - void *ReadRawData(size_t size) {return NULL;} - sptr ReadRemoteObject() {return NULL;} - int ReadFileDescriptor() {return 0;} -}; - -class MessageOption { -}; - -class IRemoteBroker { -#define DECLARE_INTERFACE_DESCRIPTOR(DESCRIPTOR) \ - static inline const std::u16string metaDescriptor_ = {DESCRIPTOR}; \ - static inline const std::u16string &GetDescriptor() \ - { \ - return metaDescriptor_; \ - } -}; - -class IRemoteObject : public virtual Parcelable { - public: - virtual int SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {return 0;} - class DeathRecipient : public RefBase { - public: - virtual void OnRemoteDied(const wptr &object) {}; - }; - virtual bool RemoveDeathRecipient(const sptr &recipient) {return true;}; - virtual bool AddDeathRecipient(const sptr &recipient) {return true;}; - virtual bool Marshalling(Parcel &parcel) const override {return true;} -}; - -template class IRemoteProxy: public INTERFACE, public virtual RefBase { - public: - IRemoteProxy(const sptr &object) { - } - sptr Remote() {return NULL;} -}; - -class IPCObjectStub : public IRemoteObject { - public: - virtual int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {return 0;} -}; - -template class IRemoteStub: public IPCObjectStub, public INTERFACE { - public: - virtual int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {return 0;} -}; - -class IPCSkeleton { - public: - static sptr GetContextObject() {return NULL;} - static pid_t GetCallingPid() {return 0;} - static pid_t GetCallingUid() {return 0;} -}; - -class SystemAbility { - DECLARE_BASE_SYSTEM_ABILITY(SystemAbility); - public: - static bool MakeAndRegisterAbility(SystemAbility* systemAbility) {return true;} - bool Publish(sptr systemAbility) {return true;} - protected: - SystemAbility(bool runOnCreate = false) {} - SystemAbility(int32_t systemAbilityId, bool runOnCreate = false) {} - virtual void OnStart() = 0; - virtual void OnStop() = 0; -}; - -}; -#endif - -#endif diff --git a/dsoftbus/depend/ipc/include/ipc_base.h b/dsoftbus/depend/ipc/include/ipc_base.h new file mode 100644 index 00000000..d289e510 --- /dev/null +++ b/dsoftbus/depend/ipc/include/ipc_base.h @@ -0,0 +1,56 @@ +#ifndef _IPC_BASE_H_ +#define _IPC_BASE_H_ + +#include +#include +#include +#include +#include +#include + +#define IPC_LOG(fmt, args...) \ + printf("[IPC LOG %s:%u]" fmt, __FILE__, __LINE__, ##args); + +extern key_t g_send_shm_key; +extern key_t g_receive_shm_key; + +const int IPC_SHM_FLAG = IPC_CREAT | 0666; + +const size_t DATA_SIZE = 0x20000; + +struct IpcShmData { + size_t inputSz; + size_t outputSz; + char inputData[DATA_SIZE]; + char outputData[DATA_SIZE]; + volatile bool needReply; + uint32_t requestCode; + volatile bool containFd; +}; + +static inline IpcShmData *OpenShmCommon(key_t shmKey, int flag) +{ + int shmFd = shmget(shmKey, sizeof(IpcShmData), flag); + if (shmFd < 0) { + IPC_LOG("Get shm failed\n"); + return nullptr; + } + void *shmPtr = shmat(shmFd, 0, 0); + if (shmPtr == (void *)-1) { + IPC_LOG("Map shm failed\n"); + return nullptr; + } + return (IpcShmData *)shmPtr; +} + +static inline IpcShmData *OpenShm(key_t shmKey) +{ + return OpenShmCommon(shmKey, IPC_SHM_FLAG); +} + +static inline IpcShmData *OpenShmExcl(key_t shmKey) +{ + return OpenShmCommon(shmKey, IPC_SHM_FLAG | IPC_EXCL); +} + +#endif // _IPC_BASE_H_ diff --git a/dsoftbus/depend/ipc/include/ipc_center.h b/dsoftbus/depend/ipc/include/ipc_center.h new file mode 100644 index 00000000..ebf9891c --- /dev/null +++ b/dsoftbus/depend/ipc/include/ipc_center.h @@ -0,0 +1,24 @@ +#ifndef _IPC_CENTER_H_ +#define _IPC_CENTER_H_ + +#include "iremote_object.h" +#include "ipc_object_stub.h" + +namespace OHOS { + +class IpcCenter { +public: + IpcCenter(); + bool Init(bool isServer, IPCObjectStub *stub); + bool ThreadCreate(); + void ProcessHandle(); +private: + bool ShmInit(key_t ShmKey); + size_t threadNum_; + IPCObjectStub *ipcStub_; + bool needStop_; +}; + +} // namespace OHOS + +#endif //_IPC_CENTER_H_ diff --git a/dsoftbus/depend/ipc/include/ipc_object_stub.h b/dsoftbus/depend/ipc/include/ipc_object_stub.h new file mode 100644 index 00000000..ec93b6d7 --- /dev/null +++ b/dsoftbus/depend/ipc/include/ipc_object_stub.h @@ -0,0 +1,17 @@ +#ifndef _IPC_OBJECT_STUB_H_ +#define _IPC_OBJECT_STUB_H_ + +#include "iremote_object.h" + +namespace OHOS { + +class IPCObjectStub : public IRemoteObject { +public: + IPCObjectStub(); + ~IPCObjectStub(); + virtual int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option); +}; + +} // namespace OHOS + +#endif // _IPC_OBJECT_STUB_H_ \ No newline at end of file diff --git a/dsoftbus/depend/ipc/include/ipc_skeleton.h b/dsoftbus/depend/ipc/include/ipc_skeleton.h index f2e4d534..45260cbf 100644 --- a/dsoftbus/depend/ipc/include/ipc_skeleton.h +++ b/dsoftbus/depend/ipc/include/ipc_skeleton.h @@ -1 +1,34 @@ -#include "depend.h" +#ifndef _IPC_SKELETON_H_ +#define _IPC_SKELETON_H_ + +#include +#include + +#include "parcel.h" +#include "refbase.h" + +namespace OHOS { + +class IRemoteObject; + +class IPCSkeleton { +public: + IPCSkeleton() = default; + ~IPCSkeleton() = default; + static pid_t GetCallingPid(); + static uid_t GetCallingUid(); + static sptr< IRemoteObject > GetContextObject(); + static bool SetContextObject(sptr< IRemoteObject > &object); + static bool SocketListening(bool isServer); + static int SocketReadFd(); + static bool SocketWriteFd(int fd); + +private: + static sptr< IRemoteObject > obj_; + static int socketFd_; + static bool isServer_; +}; + +} // namespace OHOS + +#endif // _IPC_SKELETON_H_ diff --git a/dsoftbus/depend/ipc/include/ipc_types.h b/dsoftbus/depend/ipc/include/ipc_types.h index f2e4d534..80f50cef 100644 --- a/dsoftbus/depend/ipc/include/ipc_types.h +++ b/dsoftbus/depend/ipc/include/ipc_types.h @@ -1 +1 @@ -#include "depend.h" +/* empty head files for compile */ diff --git a/dsoftbus/depend/ipc/include/iremote_broker.h b/dsoftbus/depend/ipc/include/iremote_broker.h index f2e4d534..e6f54250 100644 --- a/dsoftbus/depend/ipc/include/iremote_broker.h +++ b/dsoftbus/depend/ipc/include/iremote_broker.h @@ -1 +1,15 @@ -#include "depend.h" +#ifndef _IREMOTE_BROKER_H +#define _IREMOTE_BROKER_H + +#ifdef __cplusplus + +namespace OHOS { + +template class BrokerDelegator { +}; + +}; + +#endif + +#endif diff --git a/dsoftbus/depend/ipc/include/iremote_object.h b/dsoftbus/depend/ipc/include/iremote_object.h index f2e4d534..a955172e 100644 --- a/dsoftbus/depend/ipc/include/iremote_object.h +++ b/dsoftbus/depend/ipc/include/iremote_object.h @@ -1 +1,42 @@ -#include "depend.h" +#ifndef _IPC_REMOTE_OBJECT_H_ +#define _IPC_REMOTE_OBJECT_H_ + +#include "message_parcel.h" +#include "message_option.h" + +namespace OHOS { + +class IRemoteBroker : public virtual RefBase { +public: + IRemoteBroker() = default; + virtual ~IRemoteBroker() override = default; + virtual sptr< IRemoteObject > AsObject() = 0; + static inline sptr< IRemoteBroker > AsImplement(const sptr< IRemoteObject > &object) + { + return nullptr; + } +}; + +#define DECLARE_INTERFACE_DESCRIPTOR(DESCRIPTOR) \ + static inline const std::u16string metaDescriptor_ = {DESCRIPTOR} ; \ + static inline const std::u16string &GetDescriptor() \ + { \ + return metaDescriptor_; \ + } + +class IRemoteObject : public virtual Parcelable { +public: + class DeathRecipient : public RefBase { + public: + virtual void OnRemoteDied(const wptr< IRemoteObject > &object) {} + }; + + virtual int SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option); + virtual bool AddDeathRecipient(const sptr< DeathRecipient > &recipient); + virtual bool RemoveDeathRecipient(const sptr< DeathRecipient > &recipient); + virtual bool Marshalling(Parcel &parcel) const override; +}; + +} // namespace OHOS + +#endif // _IPC_REMOTE_OBJECT_H_ diff --git a/dsoftbus/depend/ipc/include/iremote_parcel.h b/dsoftbus/depend/ipc/include/iremote_parcel.h index f2e4d534..80f50cef 100644 --- a/dsoftbus/depend/ipc/include/iremote_parcel.h +++ b/dsoftbus/depend/ipc/include/iremote_parcel.h @@ -1 +1 @@ -#include "depend.h" +/* empty head files for compile */ diff --git a/dsoftbus/depend/ipc/include/iremote_proxy.h b/dsoftbus/depend/ipc/include/iremote_proxy.h index f2e4d534..4078c356 100644 --- a/dsoftbus/depend/ipc/include/iremote_proxy.h +++ b/dsoftbus/depend/ipc/include/iremote_proxy.h @@ -1 +1,42 @@ -#include "depend.h" +#ifndef OHOS_IPC_IREMOTE_PROXY_H +#define OHOS_IPC_IREMOTE_PROXY_H + +#include "ipc_skeleton.h" + +namespace OHOS { +template class IRemoteProxy : public INTERFACE { +public: + explicit IRemoteProxy(const sptr &object); + ~IRemoteProxy() override = default; + sptr Remote(); + +protected: + sptr AsObject() override; + +private: + const sptr remoteObj_; +}; + +template +sptr IRemoteProxyremoteObj_ = nullptr; + +template +IRemoteProxy::IRemoteProxy(const sptr &object) : remoteObj_(object) {} + +template +sptr IRemoteProxy::AsObject() +{ + if (remoteObj_ != nullptr) { + return remoteObj_; + } + return IPCSkeleton::GetContextObject(); +} + +template +sptr IRemoteProxy::Remote() +{ + return AsObject(); +} + +} // namespece OHOS +#endif // OHOS_IPC_IREMOTE_PROXY_H \ No newline at end of file diff --git a/dsoftbus/depend/ipc/include/iremote_stub.h b/dsoftbus/depend/ipc/include/iremote_stub.h index f2e4d534..3b5184f9 100644 --- a/dsoftbus/depend/ipc/include/iremote_stub.h +++ b/dsoftbus/depend/ipc/include/iremote_stub.h @@ -1 +1,27 @@ -#include "depend.h" +#ifndef _IPC_IREMOTE_STUB_H_ +#define _IPC_IREMOTE_STUB_H_ + +#include "ipc_object_stub.h" + +namespace OHOS { + +template< typename INTERFACE > +class IRemoteStub : public IPCObjectStub, public INTERFACE { +public: + IRemoteStub(); + virtual ~IRemoteStub() = default; + sptr< IRemoteObject > AsObject() override; +}; + +template< typename INTERFACE > +IRemoteStub< INTERFACE >::IRemoteStub() : IPCObjectStub() {} + +template< typename INTERFACE > +sptr< IRemoteObject > IRemoteStub< INTERFACE >::AsObject() +{ + return this; +} + +} // namespace OHOS + +#endif // _IPC_IREMOTE_STUB_H_ \ No newline at end of file diff --git a/dsoftbus/depend/ipc/include/message_option.h b/dsoftbus/depend/ipc/include/message_option.h new file mode 100644 index 00000000..e5904b92 --- /dev/null +++ b/dsoftbus/depend/ipc/include/message_option.h @@ -0,0 +1,10 @@ +#ifndef _MESSAGE_OPTION_H_ +#define _MESSAGE_OPTION_H_ + +namespace OHOS { + +class MessageOption {}; + +} + +#endif // _MESSAGE_OPTION_H_ \ No newline at end of file diff --git a/dsoftbus/depend/ipc/include/message_parcel.h b/dsoftbus/depend/ipc/include/message_parcel.h index f2e4d534..61534dfc 100644 --- a/dsoftbus/depend/ipc/include/message_parcel.h +++ b/dsoftbus/depend/ipc/include/message_parcel.h @@ -1 +1,28 @@ -#include "depend.h" +#ifndef _MESSAGE_PARCEL_H_ +#define _MESSAGE_PARCEL_H_ + +#include "parcel.h" +#include "refbase.h" + +namespace OHOS { + +class IRemoteObject; +class MessageParcel : public Parcel { +public: + MessageParcel(); + ~MessageParcel(); + bool WriteFileDescriptor(int fd); + int ReadFileDescriptor(); + bool WriteRawData(const void *data, size_t size); + const void *ReadRawData(size_t size); + bool ContainFileDescriptors() const; + sptr< IRemoteObject > ReadRemoteObject(); + bool WriteRemoteObject(const sptr< IRemoteObject > &object); +private: + static constexpr size_t MAX_RAWDATA_SIZE = 128 * 1024 * 1024; // 128M + int fd_; +}; + +} //namespace OHOS + +#endif // _MESSAGE_PARCEL_H_ diff --git a/dsoftbus/depend/ipc/include/system_ability.h b/dsoftbus/depend/ipc/include/system_ability.h index f2e4d534..8d893481 100644 --- a/dsoftbus/depend/ipc/include/system_ability.h +++ b/dsoftbus/depend/ipc/include/system_ability.h @@ -1 +1,44 @@ -#include "depend.h" +#ifndef _SYSTEM_ABILITY_H +#define _SYSTEM_ABILITY_H + +#ifdef __cplusplus +//#include +//#include +#include +//#include +#include "parcel.h" + +#define DECLARE_SYSTEM_ABILITY(className) \ + public: \ + virtual std::string GetClassName() override { \ + return #className; \ + } + +#define DECLARE_BASE_SYSTEM_ABILITY(className) \ + public: \ + virtual std::string GetClassName() = 0; + +namespace OHOS { + +#define REGISTER_SYSTEM_ABILITY_BY_ID(abilityClassName, systemAbilityId, runOnCreate) \ + const bool abilityClassName##_##RegisterResult = \ + SystemAbility::MakeAndRegisterAbility(new abilityClassName(systemAbilityId, runOnCreate)); + +class IRemoteObject; + +class SystemAbility { + DECLARE_BASE_SYSTEM_ABILITY(SystemAbility); + public: + static bool MakeAndRegisterAbility(SystemAbility* systemAbility) {return true;} + bool Publish(sptr systemAbility) {return true;} + protected: + SystemAbility(bool runOnCreate = false) {} + SystemAbility(int32_t systemAbilityId, bool runOnCreate = false) {} + virtual void OnStart() = 0; + virtual void OnStop() = 0; +}; + +}; +#endif + +#endif diff --git a/dsoftbus/depend/ipc/include/system_ability_definition.h b/dsoftbus/depend/ipc/include/system_ability_definition.h index f2e4d534..5634882e 100644 --- a/dsoftbus/depend/ipc/include/system_ability_definition.h +++ b/dsoftbus/depend/ipc/include/system_ability_definition.h @@ -1 +1,15 @@ -#include "depend.h" +#ifndef _SYSTEM_ABILITY_DEF_H +#define _SYSTEM_ABILITY_DEF_H + +#ifdef __cplusplus + +namespace OHOS { + +enum { + SOFTBUS_SERVER_SA_ID = 4700, +}; + +}; +#endif + +#endif diff --git a/dsoftbus/depend/ipc/ipc_center.cpp b/dsoftbus/depend/ipc/ipc_center.cpp new file mode 100644 index 00000000..b51f39d1 --- /dev/null +++ b/dsoftbus/depend/ipc/ipc_center.cpp @@ -0,0 +1,99 @@ +#include +#include + +#include "ipc_base.h" +#include "ipc_center.h" +#include "ipc_skeleton.h" + +namespace OHOS { + +IpcCenter::IpcCenter() : threadNum_(0), needStop_(false) {} + +bool IpcCenter::ShmInit(key_t shmKey) +{ + IpcShmData *shmPtr = OpenShm(shmKey); + if (shmPtr == nullptr) { + IPC_LOG("Create shm with key=0x%x\n", shmKey); + return false; + } + shmPtr->needReply = false; + shmPtr->containFd = false; + shmdt((void *)shmPtr); + return true; +} + +bool IpcCenter::Init(bool isServer, IPCObjectStub *stub) +{ + if (stub == nullptr) { + IPC_LOG("Invalid stub\n"); + return false; + } + + if (isServer && (!ShmInit(g_send_shm_key) || !ShmInit(g_receive_shm_key))) { + IPC_LOG("Shm inti failed\n"); + return false; + } + + if (isServer) { + std::swap(g_send_shm_key, g_receive_shm_key); + } + + ipcStub_ = stub; + + if (!IPCSkeleton::SocketListening(isServer)) { + IPC_LOG("Starting socket listen failed\n"); + return false; + } + + return ThreadCreate(); +} + +void IpcCenter::ProcessHandle() +{ + do { + IpcShmData *shmPtr = OpenShm(g_receive_shm_key); + if (shmPtr == nullptr) { + return; + } + while (!shmPtr->needReply) { + usleep(10); + } + MessageParcel data, reply; + MessageOption option; + data.WriteUnpadBuffer(shmPtr->inputData, shmPtr->inputSz); + if (shmPtr->containFd) { + shmPtr->containFd = false; + if (!data.WriteFileDescriptor(IPCSkeleton::SocketReadFd())) { + IPC_LOG("Process file descriptor failed"); + shmdt((void *)shmPtr); + return; + } + } + ipcStub_->OnRemoteRequest(shmPtr->requestCode, data, reply, option); + shmPtr->outputSz = reply.GetDataSize(); + memcpy(shmPtr->outputData, (void*)reply.GetData(), shmPtr->outputSz); + if (reply.ContainFileDescriptors()) { + if (!IPCSkeleton::SocketWriteFd(reply.ReadFileDescriptor())) { + IPC_LOG("Send file descriptor in reply failed\n") + shmdt((void *)shmPtr); + return; + } + shmPtr->containFd = true; + } + shmPtr->needReply = false; + shmdt((void*)shmPtr); + } while (!needStop_); +} + +bool IpcCenter::ThreadCreate() +{ + if (!threadNum_) { + ++threadNum_; + std::thread new_thread(std::bind(&IpcCenter::ProcessHandle, this)); + new_thread.detach(); + return true; + } + return false; +} + +} // namespace OHOS diff --git a/dsoftbus/depend/ipc/ipc_object_stub.cpp b/dsoftbus/depend/ipc/ipc_object_stub.cpp new file mode 100644 index 00000000..7cae93f1 --- /dev/null +++ b/dsoftbus/depend/ipc/ipc_object_stub.cpp @@ -0,0 +1,16 @@ +#include "ipc_base.h" +#include "ipc_object_stub.h" + +namespace OHOS { + +IPCObjectStub::IPCObjectStub() {} + +IPCObjectStub::~IPCObjectStub() {} + +int IPCObjectStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + IPC_LOG("IPCObjectStub::OnRemoteRequest Called\n"); + return -1; +} + +} // namespace OHOS diff --git a/dsoftbus/depend/ipc/ipc_skeleton.cpp b/dsoftbus/depend/ipc/ipc_skeleton.cpp new file mode 100644 index 00000000..9f70d36d --- /dev/null +++ b/dsoftbus/depend/ipc/ipc_skeleton.cpp @@ -0,0 +1,174 @@ +#include +#include +#include + +#include "ipc_base.h" +#include "iremote_object.h" +#include "ipc_skeleton.h" + +namespace OHOS { + +const char *IPC_SERVER_SOCKET_ADDR = "/tmp/ipc.socket.server"; +const char *IPC_CLIENT_SOCKET_ADDR = "/tmp/ipc.socket.client"; + +sptr< IRemoteObject > IPCSkeleton::obj_ = nullptr; +int IPCSkeleton::socketFd_ = -1; +bool IPCSkeleton::isServer_ = true; + +pid_t IPCSkeleton::GetCallingPid() +{ + return getpid(); +} + +uid_t IPCSkeleton::GetCallingUid() +{ + return getuid(); +} + +bool IPCSkeleton::SetContextObject(sptr< IRemoteObject > &object) +{ + obj_ = object; + return true; +} + +sptr< IRemoteObject > IPCSkeleton::GetContextObject() +{ + if (obj_ == nullptr) { + obj_ = new IRemoteObject(); + } + return obj_; +} + +bool IPCSkeleton::SocketListening(bool isServer) +{ + if (socketFd_ >= 0) { + IPC_LOG("Socket is opened\n"); + return false; + } + + isServer_ = isServer; + + const char *ipcPath = isServer ? IPC_SERVER_SOCKET_ADDR : IPC_CLIENT_SOCKET_ADDR; + unlink(ipcPath); + + socketFd_ = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0); + if (socketFd_ < 0) { + IPC_LOG("Socket failed errno=%d\n", errno); + return false; + } + + struct sockaddr_un socketAddr; + memset(&socketAddr, 0, sizeof(socketAddr)); + socketAddr.sun_family = AF_UNIX; + strcpy(socketAddr.sun_path, ipcPath); + int ret = bind(socketFd_, (struct sockaddr *)&socketAddr, sizeof(socketAddr)); + if (ret < 0) { + IPC_LOG("Bind socket failed errno=%d\n", errno); + close(socketFd_); + socketFd_ = -1; + return false; + } + + ret = listen(socketFd_, 3); + if (ret < 0) { + IPC_LOG("listen socket failed errno=%d\n", errno); + close(socketFd_); + socketFd_ = -1; + return false; + } + return true; +} + +int IPCSkeleton::SocketReadFd() +{ + if (socketFd_ < 0) { + IPC_LOG("Read fd from an uninitialized socket\n"); + return -1; + } + + struct sockaddr_un acceptAddr; + socklen_t sockLen = sizeof(acceptAddr); + int recvFd = accept(socketFd_, (struct sockaddr *)&acceptAddr, &sockLen); + if (recvFd < 0) { + IPC_LOG("Accept failed errno=%d\n", errno); + return -1; + } + + struct msghdr msg; + struct iovec iov[1]; + char buf[100] = ""; + msg.msg_name = nullptr; + msg.msg_namelen = 0; + iov[0].iov_base = buf; + iov[0].iov_len = 100; + msg.msg_iov = iov; + msg.msg_iovlen = 1; + msg.msg_flags = 0; + char cm[CMSG_LEN(sizeof(int))]; + msg.msg_control = cm; + msg.msg_controllen = CMSG_LEN(sizeof(int)); + int ret = recvmsg(recvFd, &msg, 0); + if (ret < 0) { + IPC_LOG("Receive error, errno=%d\n", errno); + close(recvFd); + return -1; + } + + struct cmsghdr *cmsgPtr = CMSG_FIRSTHDR(&msg); + if (cmsgPtr == nullptr || cmsgPtr->cmsg_len != CMSG_LEN(sizeof(int)) || + cmsgPtr->cmsg_level != SOL_SOCKET || + cmsgPtr->cmsg_type != SCM_RIGHTS) { + IPC_LOG("Received wrong data\n"); + close(recvFd); + return -1; + } + close(recvFd); + return *((int *)CMSG_DATA(cmsgPtr)); +} + +bool IPCSkeleton::SocketWriteFd(int fd) +{ + int socketFd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0); + if (socketFd < 0) { + IPC_LOG("Socket failed errno=%d\n", errno); + return false; + } + + struct sockaddr_un socketAddr; + memset(&socketAddr, 0, sizeof(socketAddr)); + socketAddr.sun_family = AF_UNIX; + strcpy(socketAddr.sun_path, isServer_ ? IPC_CLIENT_SOCKET_ADDR : IPC_SERVER_SOCKET_ADDR); + int ret = connect(socketFd, (struct sockaddr *)&socketAddr, sizeof(socketAddr)); + if (ret < 0) { + IPC_LOG("Connect failed errno=%d\n", errno); + close(socketFd); + return false; + } + + struct msghdr msg; + struct iovec iov[1]; + char buf[100] = "IPC Socket Data with File Descriptor"; + msg.msg_name = nullptr; + msg.msg_namelen = 0; + iov[0].iov_base = buf; + iov[0].iov_len = 100; + msg.msg_iov = iov; + msg.msg_iovlen = 1; + msg.msg_flags = 0; + char cm[CMSG_LEN(sizeof(int))]; + msg.msg_control = cm; + msg.msg_controllen = CMSG_LEN(sizeof(int)); + struct cmsghdr *cmsgPtr = CMSG_FIRSTHDR(&msg); + cmsgPtr->cmsg_len = CMSG_LEN(sizeof(int)); + cmsgPtr->cmsg_level = SOL_SOCKET; + cmsgPtr->cmsg_type = SCM_RIGHTS; + *((int *)CMSG_DATA(cmsgPtr)) = fd; + ret = sendmsg(socketFd, &msg, 0); + if (ret < 0) { + IPC_LOG("Send failed errno=%d\n", errno); + } + close(socketFd); + return ret >= 0; +} + +} //namespace OHOS diff --git a/dsoftbus/depend/ipc/iremote_object.cpp b/dsoftbus/depend/ipc/iremote_object.cpp new file mode 100644 index 00000000..055258d7 --- /dev/null +++ b/dsoftbus/depend/ipc/iremote_object.cpp @@ -0,0 +1,70 @@ +#include "ipc_base.h" +#include "iremote_object.h" +#include "ipc_skeleton.h" + +key_t g_send_shm_key = 0x544F53; +key_t g_receive_shm_key = 0x52544F; + +namespace OHOS { + +const int32_t GET_SA_REQUEST_CODE = 2; + +bool IRemoteObject::AddDeathRecipient(const sptr< DeathRecipient > &recipient) +{ + return true; +} + +bool IRemoteObject::RemoveDeathRecipient(const sptr< DeathRecipient > &recipient) +{ + return true; +} + +int IRemoteObject::SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + IpcShmData *shmPtr = NULL; + + if (code == GET_SA_REQUEST_CODE) { + return 0; + } + + shmPtr = OpenShm(g_send_shm_key); + if (shmPtr == nullptr) { + return -1; + } + + // waiting previous ipc + while (shmPtr->needReply); + + shmPtr->requestCode = code; + shmPtr->inputSz = data.GetDataSize(); + memcpy(shmPtr->inputData, (void *)data.GetData(), shmPtr->inputSz); + if (data.ContainFileDescriptors()) { + shmPtr->containFd = true; + if (!IPCSkeleton::SocketWriteFd(data.ReadFileDescriptor())) { + IPC_LOG("Send File Descriptor failed\n"); + shmdt((void*)shmPtr); + return -1; + } + } + shmPtr->needReply = true; + + while (shmPtr->needReply); + reply.WriteUnpadBuffer(shmPtr->outputData, shmPtr->outputSz); + if (shmPtr->containFd) { + if (!reply.WriteFileDescriptor(IPCSkeleton::SocketReadFd())) { + IPC_LOG("Reveive reply fd failed"); + shmdt((void *)shmPtr); + return -1; + } + shmPtr->containFd = false; + } + shmdt((void *)shmPtr); + return 0; +} + +bool IRemoteObject::Marshalling(Parcel &parcel) const +{ + return true; +} + +} // namespace OHOS diff --git a/dsoftbus/depend/ipc/message_parcel.cpp b/dsoftbus/depend/ipc/message_parcel.cpp new file mode 100644 index 00000000..02253efc --- /dev/null +++ b/dsoftbus/depend/ipc/message_parcel.cpp @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#include "ipc_base.h" +#include "message_parcel.h" +#include "ipc_skeleton.h" +#include "iremote_object.h" + +namespace OHOS { + +MessageParcel::MessageParcel() : Parcel(), fd_(-1) {} + +MessageParcel::~MessageParcel() {} + +bool MessageParcel::WriteFileDescriptor(int fd) +{ + if (fd < 0) { + return false; + } + + int dupFd = dup(fd); + if (dupFd < 0) { + return false; + } + + fd_ = dupFd; + + return true; +} + +int MessageParcel::ReadFileDescriptor() +{ + return fd_; +} + +bool MessageParcel::ContainFileDescriptors() const +{ + return fd_ >= 0; +} + +bool MessageParcel::WriteRawData(const void *data, size_t size) +{ + if (data == nullptr || size > MAX_RAWDATA_SIZE) { + return false; + } + if (!WriteInt32(size)) { + return false; + } + + return WriteUnpadBuffer(data, size); +} + +const void *MessageParcel::ReadRawData(size_t size) +{ + int32_t bufferSize = ReadInt32(); + if (static_cast< unsigned int >(bufferSize) != size) { + return nullptr; + } + return ReadUnpadBuffer(size); +} + +sptr< IRemoteObject > MessageParcel::ReadRemoteObject() +{ + return IPCSkeleton::GetContextObject(); +} + +bool MessageParcel::WriteRemoteObject(const sptr< IRemoteObject > &object) +{ + return true; +} + +} //namespace OHOS -- Gitee