blob: 3f91eb0405fdc616cc012797f16d5766a7432c9e [file] [log] [blame]
Steven Moreland3903f462017-01-13 12:57:00 -08001/*
2 * Copyright (C) 2016 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#define LOG_TAG "libhidltransport"
17
18#include <hidl/LegacySupport.h>
19
20#include <condition_variable>
21#include <cutils/properties.h>
22#include <mutex>
23#include <utils/misc.h>
24#include <utils/Log.h>
25
26namespace android {
27namespace hardware {
28
29static const char* kDataProperty = "vold.post_fs_data_done";
30static std::mutex gDataMutex;
31static std::condition_variable gDataCondition;
32static bool gDataDone = false;
33static bool gDataStarted = false;
34
35static void voldDecryptCallback() {
36 std::lock_guard<std::mutex> lock(gDataMutex);
37
38 if (gDataDone) {
39 return; // TODO: add remove_sysprop_change_callback
40 }
41
42 // more expensive to query property_get_bool than to check the lock
43 if (!property_get_bool(kDataProperty, false)) {
44 return; // other sysprop set, and this hasn't been set yet
45 }
46
47 // file system is mounted!
48
49 gDataDone = true;
50 gDataCondition.notify_all();
51}
52
53void waitForData() {
54 {
55 std::unique_lock<std::mutex> lock(gDataMutex);
56
57 // unlikely, but we should make sure two threads in the same process
58 // don't call this method at the same time
59 if (gDataStarted) {
60 // just wait to be notified
61 gDataCondition.wait(lock, [] () {
62 return gDataDone;
63 });
64 return;
65 }
66 }
67
68 add_sysprop_change_callback(voldDecryptCallback, 0 /* priority */);
69
70 // if it already got setup before we registered the decrypt callback,
71 // we must still wake up.
72 voldDecryptCallback();
73
74 std::unique_lock<std::mutex> lock(gDataMutex);
75 gDataCondition.wait(lock, [] () {
76 return gDataDone;
77 });
78
79 gDataStarted = true;
80}
81
82namespace details {
83
84bool blockingHalBinderizationEnabled() {
85 waitForData();
86 return property_get_bool("persist.hal.binderization", false);
87}
88
89void blockIfBinderizationDisabled(const std::string& interface,
90 const std::string& instance) {
91 // TODO(b/34274385) remove this
92 // Must wait for data to be mounted and persistant properties to be read,
93 // but only delay the start of hals which require reading this property.
94 bool enabled = blockingHalBinderizationEnabled();
95
96 if (!enabled) {
97 ALOGI("Deactivating %s/%s binderized service to"
98 " yield to passthrough implementation.",
99 interface.c_str(),
100 instance.c_str());
101 while (true) {
102 sleep(UINT_MAX);
103 }
104 }
105}
106} // namespace details
107
108} // namespace hardware
109} // namespace android