• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 #include <hidl/HidlBinderSupport.h>
17 #include <hidl/HidlTransportSupport.h>
18 #include <hidl/Static.h>
19 
20 #include <android-base/logging.h>
21 #include <android/hidl/manager/1.0/IServiceManager.h>
22 
23 namespace android {
24 namespace hardware {
25 
26 using ::android::hidl::base::V1_0::IBase;
27 
configureRpcThreadpool(size_t maxThreads,bool callerWillJoin)28 void configureRpcThreadpool(size_t maxThreads, bool callerWillJoin) {
29     // TODO(b/32756130) this should be transport-dependent
30     configureBinderRpcThreadpool(maxThreads, callerWillJoin);
31 }
joinRpcThreadpool()32 void joinRpcThreadpool() {
33     // TODO(b/32756130) this should be transport-dependent
34     joinBinderRpcThreadpool();
35 }
36 
setupTransportPolling()37 int setupTransportPolling() {
38     return setupBinderPolling();
39 }
40 
handleTransportPoll(int)41 status_t handleTransportPoll(int /*fd*/) {
42     return handleBinderPoll();
43 }
44 
45 // TODO(b/122472540): only store one data item per object
46 template <typename V>
pruneMapLocked(ConcurrentMap<wp<IBase>,V> & map)47 static void pruneMapLocked(ConcurrentMap<wp<IBase>, V>& map) {
48     std::vector<wp<IBase>> toDelete;
49     for (const auto& kv : map) {
50         if (kv.first.promote() == nullptr) {
51             toDelete.push_back(kv.first);
52         }
53     }
54     for (const auto& k : toDelete) {
55         map.eraseLocked(k);
56     }
57 }
58 
setMinSchedulerPolicy(const sp<IBase> & service,int policy,int priority)59 bool setMinSchedulerPolicy(const sp<IBase>& service, int policy, int priority) {
60     if (service->isRemote()) {
61         LOG(ERROR) << "Can't set scheduler policy on remote service.";
62         return false;
63     }
64 
65     switch (policy) {
66         case SCHED_NORMAL: {
67             if (priority < -20 || priority > 19) {
68                 LOG(ERROR) << "Invalid priority for SCHED_NORMAL: " << priority;
69                 return false;
70             }
71         } break;
72         case SCHED_RR:
73         case SCHED_FIFO: {
74             if (priority < 1 || priority > 99) {
75                 LOG(ERROR) << "Invalid priority for " << policy << " policy: " << priority;
76                 return false;
77             }
78         } break;
79         default: {
80             LOG(ERROR) << "Invalid scheduler policy " << policy;
81             return false;
82         }
83     }
84 
85     // Due to ABI considerations, IBase cannot have a destructor to clean this up.
86     // So, because this API is so infrequently used, (expected to be usually only
87     // one time for a process, but it can be more), we are cleaning it up here.
88     std::unique_lock<std::mutex> lock = details::gServicePrioMap->lock();
89     pruneMapLocked(details::gServicePrioMap.get());
90     details::gServicePrioMap->setLocked(service, {policy, priority});
91 
92     return true;
93 }
94 
setRequestingSid(const sp<IBase> & service,bool requesting)95 bool setRequestingSid(const sp<IBase>& service, bool requesting) {
96     if (service->isRemote()) {
97         LOG(ERROR) << "Can't set requesting sid on remote service.";
98         return false;
99     }
100 
101     // Due to ABI considerations, IBase cannot have a destructor to clean this up.
102     // So, because this API is so infrequently used, (expected to be usually only
103     // one time for a process, but it can be more), we are cleaning it up here.
104     std::unique_lock<std::mutex> lock = details::gServiceSidMap->lock();
105     pruneMapLocked(details::gServiceSidMap.get());
106     details::gServiceSidMap->setLocked(service, requesting);
107 
108     return true;
109 }
110 
interfacesEqual(const sp<IBase> & left,const sp<IBase> & right)111 bool interfacesEqual(const sp<IBase>& left, const sp<IBase>& right) {
112     if (left == nullptr || right == nullptr || !left->isRemote() || !right->isRemote()) {
113         return left == right;
114     }
115     return getOrCreateCachedBinder(left.get()) == getOrCreateCachedBinder(right.get());
116 }
117 
118 namespace details {
getPidIfSharable()119 int32_t getPidIfSharable() {
120 #if LIBHIDL_TARGET_DEBUGGABLE
121     return getpid();
122 #else
123     using android::hidl::manager::V1_0::IServiceManager;
124     return static_cast<int32_t>(IServiceManager::PidConstant::NO_PID);
125 #endif
126 }
127 }  // namespace details
128 
129 }  // namespace hardware
130 }  // namespace android
131