1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <atomic> 20 #include <stdint.h> 21 #include <binder/IBinder.h> 22 23 // --------------------------------------------------------------------------- 24 namespace android { 25 26 namespace internal { 27 class Stability; 28 } 29 30 class BBinder : public IBinder 31 { 32 public: 33 BBinder(); 34 35 virtual const String16& getInterfaceDescriptor() const; 36 virtual bool isBinderAlive() const; 37 virtual status_t pingBinder(); 38 virtual status_t dump(int fd, const Vector<String16>& args); 39 40 // NOLINTNEXTLINE(google-default-arguments) 41 virtual status_t transact( uint32_t code, 42 const Parcel& data, 43 Parcel* reply, 44 uint32_t flags = 0) final; 45 46 // NOLINTNEXTLINE(google-default-arguments) 47 virtual status_t linkToDeath(const sp<DeathRecipient>& recipient, 48 void* cookie = nullptr, 49 uint32_t flags = 0); 50 51 // NOLINTNEXTLINE(google-default-arguments) 52 virtual status_t unlinkToDeath( const wp<DeathRecipient>& recipient, 53 void* cookie = nullptr, 54 uint32_t flags = 0, 55 wp<DeathRecipient>* outRecipient = nullptr); 56 57 virtual void* attachObject(const void* objectID, void* object, void* cleanupCookie, 58 object_cleanup_func func) final; 59 virtual void* findObject(const void* objectID) const final; 60 virtual void* detachObject(const void* objectID) final; 61 void withLock(const std::function<void()>& doWithLock); 62 63 virtual BBinder* localBinder(); 64 65 bool isRequestingSid(); 66 // This must be called before the object is sent to another process. Not thread safe. 67 void setRequestingSid(bool requestSid); 68 69 sp<IBinder> getExtension(); 70 // This must be called before the object is sent to another process. Not thread safe. 71 void setExtension(const sp<IBinder>& extension); 72 73 // This must be called before the object is sent to another process. Not thread safe. 74 // 75 // This function will abort if improper parameters are set. This is like 76 // sched_setscheduler. However, it sets the minimum scheduling policy 77 // only for the duration that this specific binder object is handling the 78 // call in a threadpool. By default, this API is set to SCHED_NORMAL/0. In 79 // this case, the scheduling priority will not actually be modified from 80 // binder defaults. See also IPCThreadState::disableBackgroundScheduling. 81 // 82 // Appropriate values are: 83 // SCHED_NORMAL: -20 <= priority <= 19 84 // SCHED_RR/SCHED_FIFO: 1 <= priority <= 99 85 void setMinSchedulerPolicy(int policy, int priority); 86 int getMinSchedulerPolicy(); 87 int getMinSchedulerPriority(); 88 89 // Whether realtime scheduling policies are inherited. 90 bool isInheritRt(); 91 // This must be called before the object is sent to another process. Not thread safe. 92 void setInheritRt(bool inheritRt); 93 94 pid_t getDebugPid(); 95 96 // Whether this binder has been sent to another process. 97 bool wasParceled(); 98 // Consider this binder as parceled (setup/init-related calls should no 99 // longer by called. This is automatically set by when this binder is sent 100 // to another process. 101 void setParceled(); 102 103 [[nodiscard]] status_t setRpcClientDebug(android::base::unique_fd clientFd, 104 const sp<IBinder>& keepAliveBinder); 105 106 protected: 107 virtual ~BBinder(); 108 109 // NOLINTNEXTLINE(google-default-arguments) 110 virtual status_t onTransact( uint32_t code, 111 const Parcel& data, 112 Parcel* reply, 113 uint32_t flags = 0); 114 115 private: 116 BBinder(const BBinder& o); 117 BBinder& operator=(const BBinder& o); 118 119 class RpcServerLink; 120 class Extras; 121 122 Extras* getOrCreateExtras(); 123 124 [[nodiscard]] status_t setRpcClientDebug(const Parcel& data); 125 void removeRpcServerLink(const sp<RpcServerLink>& link); 126 127 std::atomic<Extras*> mExtras; 128 129 friend ::android::internal::Stability; 130 int16_t mStability; 131 bool mParceled; 132 uint8_t mReserved0; 133 134 #ifdef __LP64__ 135 int32_t mReserved1; 136 #endif 137 }; 138 139 // --------------------------------------------------------------------------- 140 141 class BpRefBase : public virtual RefBase 142 { 143 protected: 144 explicit BpRefBase(const sp<IBinder>& o); 145 virtual ~BpRefBase(); 146 virtual void onFirstRef(); 147 virtual void onLastStrongRef(const void* id); 148 virtual bool onIncStrongAttempted(uint32_t flags, const void* id); 149 remote()150 inline IBinder* remote() const { return mRemote; } remoteStrong()151 inline sp<IBinder> remoteStrong() const { return sp<IBinder>::fromExisting(mRemote); } 152 153 private: 154 BpRefBase(const BpRefBase& o); 155 BpRefBase& operator=(const BpRefBase& o); 156 157 IBinder* const mRemote; 158 RefBase::weakref_type* mRefs; 159 std::atomic<int32_t> mState; 160 }; 161 162 } // namespace android 163 164 // --------------------------------------------------------------------------- 165