• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_IPC_DBINDER_DATABUS_INVOKER_H
17 #define OHOS_IPC_DBINDER_DATABUS_INVOKER_H
18 
19 #include <unistd.h>
20 #include <sys/types.h>
21 #include "hilog/log.h"
22 #include "dbinder_base_invoker.h"
23 #include "invoker_factory.h"
24 #include "dbinder_session_object.h"
25 #include "sys_binder.h"
26 
27 namespace OHOS {
28 class DBinderDatabusInvoker : public DBinderBaseInvoker<DBinderSessionObject> {
29 public:
30     DBinderDatabusInvoker();
31     ~DBinderDatabusInvoker();
32     bool AcquireHandle(int32_t handle) override;
33     bool ReleaseHandle(int32_t handle) override;
34     void JoinThread(bool initiative) override;
35     void JoinProcessThread(bool initiative) override;
36     void StopWorkThread() override;
37     bool FlattenObject(Parcel &parcel, const IRemoteObject *object) const override;
38     sptr<IRemoteObject> UnflattenObject(Parcel &parcel) override;
39     int ReadFileDescriptor(Parcel &parcel) override;
40     bool WriteFileDescriptor(Parcel &parcel, int fd, bool takeOwnership) override;
41     std::string GetCallerSid() const override;
42     pid_t GetCallerPid() const override;
43     pid_t GetCallerRealPid() const override;
44     uid_t GetCallerUid() const override;
45     uint64_t GetCallerTokenID() const override;
46     uint64_t GetFirstCallerTokenID() const override;
47     uint64_t GetSelfTokenID() const override;
48     uint64_t GetSelfFirstCallerTokenID() const override;
49     uint32_t GetStatus() override;
50     virtual int32_t GetClientFd() const override;
51     bool IsLocalCalling() override;
52     std::string GetLocalDeviceID() override;
53     std::string GetCallerDeviceID() const override;
54 
55     bool UpdateClientSession(std::shared_ptr<DBinderSessionObject> sessionObject) override;
56     std::shared_ptr<DBinderSessionObject> QueryClientSessionObject(uint32_t databusHandle) override;
57     std::shared_ptr<DBinderSessionObject> QueryServerSessionObject(uint32_t handle) override;
58     std::shared_ptr<DBinderSessionObject> CreateServerSessionObject(binder_uintptr_t binder,
59         std::shared_ptr<DBinderSessionObject> sessionObject) override;
60     int FlushCommands(IRemoteObject *object) override;
61 
62     void OnDatabusSessionServerSideClosed(int32_t socketId);
63     void OnDatabusSessionClientSideClosed(int32_t socketId);
64 
65     bool OnReceiveNewConnection(int32_t socketId, int peerPid, int peerUid,
66         std::string peerName, std::string networkId);
67     std::string ResetCallingIdentity() override;
68     bool SetCallingIdentity(std::string &identity, bool flag) override;
69     void OnMessageAvailable(int32_t socketId, const char *data, ssize_t len);
70     bool TriggerSystemIPCThreadReclaim() override;
71     bool EnableIPCThreadReclaim(bool enable) override;
72 
73 private:
74     bool CreateProcessThread() override;
75     int OnSendMessage(std::shared_ptr<DBinderSessionObject> sessionOfPeer) override;
76     int SendData(std::shared_ptr<BufferObject> sessionBuff, int32_t socketId);
77     int OnSendRawData(std::shared_ptr<DBinderSessionObject> session, const void *data, size_t size) override;
78     std::shared_ptr<DBinderSessionObject> NewSessionOfBinderProxy(uint32_t handle,
79         std::shared_ptr<DBinderSessionObject> session) override;
80     std::shared_ptr<DBinderSessionObject> GetSessionForProxy(sptr<IPCObjectProxy> ipcProxy,
81         std::shared_ptr<DBinderSessionObject> session, const std::string &localDeviceID);
82     std::shared_ptr<DBinderSessionObject> QuerySessionOfBinderProxy(uint32_t handle,
83         std::shared_ptr<DBinderSessionObject> session) override;
84     uint32_t FlattenSession(unsigned char *sessionOffset, const std::shared_ptr<DBinderSessionObject> connectSession,
85         uint32_t binderVersion) override;
86     std::shared_ptr<DBinderSessionObject> UnFlattenSession(unsigned char *sessionOffset,
87         uint32_t binderVersion) override;
88     uint32_t QueryHandleBySession(std::shared_ptr<DBinderSessionObject> session) override;
89     virtual uint64_t GetSeqNum() const override;
90     virtual void SetSeqNum(uint64_t seq) override;
91     virtual void SetClientFd(int32_t fd) override;
92     virtual void SetCallerPid(pid_t pid) override;
93     virtual void SetCallerUid(pid_t uid) override;
94     virtual void SetStatus(uint32_t status) override;
95     virtual void SetCallerDeviceID(const std::string &deviceId) override;
96     virtual void SetCallerTokenID(const uint32_t tokenId) override;
97     virtual int CheckAndSetCallerInfo(int32_t socketId, uint64_t stubIndex) override;
98     virtual void SetCallerInfo(DBinderCallerInfo &callerInfo) override;
99     virtual void GetCallerInfo(DBinderCallerInfo &callerInfo) override;
100     uint32_t HasRawDataPackage(const char *data, ssize_t len);
101     uint32_t HasCompletePackage(const char *data, uint32_t readCursor, ssize_t len);
102     void OnRawDataAvailable(int32_t socketId, uint64_t seqNumber, const char *data, uint32_t dataSize);
103     uint64_t MakeStubIndexByRemoteObject(IRemoteObject *stubObject);
104     std::shared_ptr<DBinderSessionObject> MakeDefaultServerSessionObject(uint64_t stubIndex,
105         const std::shared_ptr<DBinderSessionObject> sessionObject);
106     bool ConnectRemoteObject2Session(IRemoteObject *stubObject, uint64_t stubIndex,
107         const std::shared_ptr<DBinderSessionObject> sessionObject);
108     bool AuthSession2Proxy(uint32_t handle, const std::shared_ptr<DBinderSessionObject> session);
109 
110 private:
111     DISALLOW_COPY_AND_MOVE(DBinderDatabusInvoker);
112     bool stopWorkThread_;
113     pid_t callerPid_;
114     pid_t callerUid_;
115     std::string callerDeviceID_;
116     uint64_t callerTokenID_;
117     uint64_t firstTokenID_;
118     uint64_t seqNumber_ = 0;
119     int32_t clientFd_ = 0;
120     uint32_t status_;
121     static constexpr int ACCESS_TOKEN_MAX_LEN = 20;
122     static inline InvokerDelegator<DBinderDatabusInvoker> DBinderDatabusDelegator_ = { IRemoteObject::IF_PROT_DATABUS };
123 };
124 } // namespace OHOS
125 #endif // OHOS_IPC_DBINDER_DATABUS_INVOKER_H
126