• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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     int TranslateIRemoteObject(int32_t cmd, const sptr<IRemoteObject> &obj) override;
70     void OnMessageAvailable(int32_t socketId, const char *data, ssize_t len);
71     bool TriggerSystemIPCThreadReclaim() override;
72     bool EnableIPCThreadReclaim(bool enable) override;
73 
74 private:
75     bool CreateProcessThread() override;
76     int OnSendMessage(std::shared_ptr<DBinderSessionObject> sessionOfPeer) override;
77     int SendData(std::shared_ptr<BufferObject> sessionBuff, int32_t socketId);
78     int OnSendRawData(std::shared_ptr<DBinderSessionObject> session, const void *data, size_t size) override;
79     std::shared_ptr<DBinderSessionObject> NewSessionOfBinderProxy(uint32_t handle,
80         std::shared_ptr<DBinderSessionObject> session) override;
81     std::shared_ptr<DBinderSessionObject> GetSessionForProxy(sptr<IPCObjectProxy> ipcProxy,
82         std::shared_ptr<DBinderSessionObject> session, const std::string &localDeviceID);
83     std::shared_ptr<DBinderSessionObject> QuerySessionOfBinderProxy(uint32_t handle,
84         std::shared_ptr<DBinderSessionObject> session) override;
85     uint32_t FlattenSession(unsigned char *sessionOffset, const std::shared_ptr<DBinderSessionObject> connectSession,
86         uint32_t binderVersion) override;
87     std::shared_ptr<DBinderSessionObject> UnFlattenSession(unsigned char *sessionOffset,
88         uint32_t binderVersion) override;
89     uint32_t QueryHandleBySession(std::shared_ptr<DBinderSessionObject> session) override;
90     virtual uint64_t GetSeqNum() const override;
91     virtual void SetSeqNum(uint64_t seq) override;
92     virtual void SetClientFd(int32_t fd) override;
93     virtual void SetCallerPid(pid_t pid) override;
94     virtual void SetCallerUid(pid_t uid) override;
95     virtual void SetStatus(uint32_t status) override;
96     virtual void SetCallerDeviceID(const std::string &deviceId) override;
97     virtual void SetCallerTokenID(const uint32_t tokenId) override;
98     virtual int CheckAndSetCallerInfo(int32_t socketId, uint64_t stubIndex) override;
99     virtual void SetCallerInfo(DBinderCallerInfo &callerInfo) override;
100     virtual void GetCallerInfo(DBinderCallerInfo &callerInfo) override;
101     uint32_t HasRawDataPackage(const char *data, ssize_t len);
102     uint32_t HasCompletePackage(const char *data, uint32_t readCursor, ssize_t len);
103     void OnRawDataAvailable(int32_t socketId, const char *data, uint32_t dataSize);
104     uint64_t MakeStubIndexByRemoteObject(IRemoteObject *stubObject);
105     std::shared_ptr<DBinderSessionObject> MakeDefaultServerSessionObject(uint64_t stubIndex,
106         const std::shared_ptr<DBinderSessionObject> sessionObject);
107     bool ConnectRemoteObject2Session(IRemoteObject *stubObject, uint64_t stubIndex,
108         const std::shared_ptr<DBinderSessionObject> sessionObject);
109     bool AuthSession2Proxy(uint32_t handle, const std::shared_ptr<DBinderSessionObject> session);
110 
111 private:
112     DISALLOW_COPY_AND_MOVE(DBinderDatabusInvoker);
113     bool stopWorkThread_;
114     pid_t callerPid_;
115     pid_t callerUid_;
116     std::string callerDeviceID_;
117     uint64_t callerTokenID_;
118     uint64_t firstTokenID_;
119     uint64_t seqNumber_ = 0;
120     int32_t clientFd_ = 0;
121     uint32_t status_;
122     static constexpr int ACCESS_TOKEN_MAX_LEN = 10;
123     static inline InvokerDelegator<DBinderDatabusInvoker> DBinderDatabusDelegator_ = { IRemoteObject::IF_PROT_DATABUS };
124 };
125 } // namespace OHOS
126 #endif // OHOS_IPC_DBINDER_DATABUS_INVOKER_H
127