xref: /aosp_15_r20/system/libhwbinder/Utils.cpp (revision 77b80299c8bdfeca3ae6d0ce27ae1ad3db289be3)
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 
file_exists(const std::string & file)30 static bool file_exists(const std::string& file) {
31     int res = access(file.c_str(), F_OK);
32     if (res == 0 || errno == EACCES) return true;
33     return false;
34 }
35 
isHwServiceManagerInstalled()36 static bool isHwServiceManagerInstalled() {
37     return file_exists("/system_ext/bin/hwservicemanager") ||
38            file_exists("/system/system_ext/bin/hwservicemanager") ||
39            file_exists("/system/bin/hwservicemanager");
40 }
41 
waitForHwServiceManager()42 static bool waitForHwServiceManager() {
43     if (!isHwServiceManagerInstalled()) {
44         return false;
45     }
46     // TODO(b/31559095): need bionic host so that we can use 'prop_info' returned
47     // from WaitForProperty
48 #ifdef __ANDROID__
49     static const char* kHwServicemanagerReadyProperty = "hwservicemanager.ready";
50 
51     using std::literals::chrono_literals::operator""s;
52 
53     using android::base::WaitForProperty;
54     while (true) {
55         if (base::GetBoolProperty("hwservicemanager.disabled", false)) {
56             return false;
57         }
58         if (WaitForProperty(kHwServicemanagerReadyProperty, "true", 1s)) {
59             return true;
60         }
61         LOG(WARNING) << "Waited for hwservicemanager.ready for a second, waiting another...";
62     }
63 #endif  // __ANDROID__
64     return true;
65 }
66 
isHwbinderSupportedBlocking()67 bool isHwbinderSupportedBlocking() {
68     return waitForHwServiceManager();
69 }
70 }   // namespace android::hardware
71