1 /*
2  * Copyright (C) 2015 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 #include "real_binder_wrapper.h"
18 
19 #include <base/logging.h>
20 #include <binder/Binder.h>
21 #include <binder/IBinder.h>
22 #include <binder/IPCThreadState.h>
23 #include <binder/IServiceManager.h>
24 
25 namespace android {
26 
27 // Class that handles binder death notifications. libbinder wants the recipient
28 // to be wrapped in sp<>, so registering RealBinderWrapper as a recipient would
29 // be awkward.
30 class RealBinderWrapper::DeathRecipient : public IBinder::DeathRecipient {
31  public:
DeathRecipient(const::base::Closure & callback)32   explicit DeathRecipient(const ::base::Closure& callback)
33       : callback_(callback) {}
34   ~DeathRecipient() = default;
35 
36   // IBinder::DeathRecipient:
binderDied(const wp<IBinder> & who)37   void binderDied(const wp<IBinder>& who) override {
38     callback_.Run();
39   }
40 
41  private:
42   // Callback to run in response to binder death.
43   ::base::Closure callback_;
44 
45   DISALLOW_COPY_AND_ASSIGN(DeathRecipient);
46 };
47 
48 RealBinderWrapper::RealBinderWrapper() = default;
49 
50 RealBinderWrapper::~RealBinderWrapper() = default;
51 
GetService(const std::string & service_name)52 sp<IBinder> RealBinderWrapper::GetService(const std::string& service_name) {
53   sp<IServiceManager> service_manager = defaultServiceManager();
54   if (!service_manager.get()) {
55     LOG(ERROR) << "Unable to get service manager";
56     return sp<IBinder>();
57   }
58   sp<IBinder> binder =
59       service_manager->checkService(String16(service_name.c_str()));
60   if (!binder.get())
61     LOG(ERROR) << "Unable to get \"" << service_name << "\" service";
62   return binder;
63 }
64 
RegisterService(const std::string & service_name,const sp<IBinder> & binder)65 bool RealBinderWrapper::RegisterService(const std::string& service_name,
66                                         const sp<IBinder>& binder) {
67   sp<IServiceManager> service_manager = defaultServiceManager();
68   if (!service_manager.get()) {
69     LOG(ERROR) << "Unable to get service manager";
70     return false;
71   }
72   status_t status = defaultServiceManager()->addService(
73       String16(service_name.c_str()), binder);
74   if (status != OK) {
75     LOG(ERROR) << "Failed to register \"" << service_name << "\" with service "
76                << "manager";
77     return false;
78   }
79   return true;
80 }
81 
CreateLocalBinder()82 sp<BBinder> RealBinderWrapper::CreateLocalBinder() {
83   return sp<BBinder>(new BBinder());
84 }
85 
RegisterForDeathNotifications(const sp<IBinder> & binder,const::base::Closure & callback)86 bool RealBinderWrapper::RegisterForDeathNotifications(
87     const sp<IBinder>& binder,
88     const ::base::Closure& callback) {
89   sp<DeathRecipient> recipient(new DeathRecipient(callback));
90   if (binder->linkToDeath(recipient) != OK) {
91     LOG(ERROR) << "Failed to register for death notifications on "
92                << binder.get();
93     return false;
94   }
95   death_recipients_[binder] = recipient;
96   return true;
97 }
98 
UnregisterForDeathNotifications(const sp<IBinder> & binder)99 bool RealBinderWrapper::UnregisterForDeathNotifications(
100     const sp<IBinder>& binder) {
101   auto it = death_recipients_.find(binder);
102   if (it == death_recipients_.end()) {
103     LOG(ERROR) << "Not registered for death notifications on " << binder.get();
104     return false;
105   }
106   if (binder->unlinkToDeath(it->second) != OK) {
107     LOG(ERROR) << "Failed to unregister for death notifications on "
108                << binder.get();
109     return false;
110   }
111   death_recipients_.erase(it);
112   return true;
113 }
114 
GetCallingUid()115 uid_t RealBinderWrapper::GetCallingUid() {
116   return IPCThreadState::self()->getCallingUid();
117 }
118 
GetCallingPid()119 pid_t RealBinderWrapper::GetCallingPid() {
120   return IPCThreadState::self()->getCallingPid();
121 }
122 
123 }  // namespace android
124