1 /*
2  * Copyright (C) 2020 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 "Utils.h"
18 #include <hwbinder/HidlSupport.h>
19 
20 #include <string.h>
21 #include <android-base/logging.h>
22 #include <android-base/properties.h>
23 
24 namespace android::hardware {
25 
zeroMemory(uint8_t * data,size_t size)26 void zeroMemory(uint8_t* data, size_t size) {
27     memset(data, 0, size);
28 }
29 
isHwServiceManagerInstalled()30 static bool isHwServiceManagerInstalled() {
31     return access("/system_ext/bin/hwservicemanager", F_OK) == 0 ||
32            access("/system/system_ext/bin/hwservicemanager", F_OK) == 0 ||
33            access("/system/bin/hwservicemanager", F_OK) == 0;
34 }
35 
waitForHwServiceManager()36 static bool waitForHwServiceManager() {
37     if (!isHwServiceManagerInstalled()) {
38         return false;
39     }
40     // TODO(b/31559095): need bionic host so that we can use 'prop_info' returned
41     // from WaitForProperty
42 #ifdef __ANDROID__
43     static const char* kHwServicemanagerReadyProperty = "hwservicemanager.ready";
44 
45     using std::literals::chrono_literals::operator""s;
46 
47     using android::base::WaitForProperty;
48     while (true) {
49         if (base::GetBoolProperty("hwservicemanager.disabled", false)) {
50             return false;
51         }
52         if (WaitForProperty(kHwServicemanagerReadyProperty, "true", 1s)) {
53             return true;
54         }
55         LOG(WARNING) << "Waited for hwservicemanager.ready for a second, waiting another...";
56     }
57 #endif  // __ANDROID__
58     return true;
59 }
60 
isHwbinderSupportedBlocking()61 bool isHwbinderSupportedBlocking() {
62     return waitForHwServiceManager();
63 }
64 }   // namespace android::hardware
65