1 /*
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
17 #define LOG_TAG "hwservicemanager"
18
19 #include <utils/Log.h>
20
21 #include <inttypes.h>
22 #include <unistd.h>
23 #include <sys/timerfd.h>
24
25 #include <android/hidl/token/1.0/ITokenManager.h>
26 #include <cutils/properties.h>
27 #include <hidl/HidlBinderSupport.h>
28 #include <hidl/HidlTransportSupport.h>
29 #include <hidl/Status.h>
30 #include <hwbinder/IPCThreadState.h>
31 #include <hwbinder/ProcessState.h>
32 #include <utils/Errors.h>
33 #include <utils/Looper.h>
34 #include <utils/StrongPointer.h>
35
36 #include "ServiceManager.h"
37 #include "TokenManager.h"
38
39 // libutils:
40 using android::sp;
41 using android::Looper;
42 using android::LooperCallback;
43
44 // libhwbinder:
45 using android::hardware::BHwBinder;
46 using android::hardware::IBinder;
47 using android::hardware::IPCThreadState;
48 using android::hardware::ProcessState;
49
50 // libhidl
51 using android::hardware::handleTransportPoll;
52 using android::hardware::setRequestingSid;
53 using android::hardware::HidlReturnRestriction;
54 using android::hardware::setProcessHidlReturnRestriction;
55 using android::hardware::setupTransportPolling;
56 using android::hardware::toBinder;
57
58 // implementations
59 using android::hidl::manager::implementation::ServiceManager;
60 using android::hidl::manager::V1_0::IServiceManager;
61 using android::hidl::token::V1_0::implementation::TokenManager;
62
63 static std::string serviceName = "default";
64
65 class HwBinderCallback : public LooperCallback {
66 public:
setupTo(const sp<Looper> & looper)67 static sp<HwBinderCallback> setupTo(const sp<Looper>& looper) {
68 sp<HwBinderCallback> cb = new HwBinderCallback;
69
70 int fdHwBinder = setupTransportPolling();
71 LOG_ALWAYS_FATAL_IF(fdHwBinder < 0, "Failed to setupTransportPolling: %d", fdHwBinder);
72
73 // Flush after setupPolling(), to make sure the binder driver
74 // knows about this thread handling commands.
75 IPCThreadState::self()->flushCommands();
76
77 int ret = looper->addFd(fdHwBinder,
78 Looper::POLL_CALLBACK,
79 Looper::EVENT_INPUT,
80 cb,
81 nullptr /*data*/);
82 LOG_ALWAYS_FATAL_IF(ret != 1, "Failed to add binder FD to Looper");
83
84 return cb;
85 }
86
handleEvent(int fd,int,void *)87 int handleEvent(int fd, int /*events*/, void* /*data*/) override {
88 handleTransportPoll(fd);
89 return 1; // Continue receiving callbacks.
90 }
91 };
92
93 // LooperCallback for IClientCallback
94 class ClientCallbackCallback : public LooperCallback {
95 public:
setupTo(const sp<Looper> & looper,const sp<ServiceManager> & manager)96 static sp<ClientCallbackCallback> setupTo(const sp<Looper>& looper, const sp<ServiceManager>& manager) {
97 sp<ClientCallbackCallback> cb = new ClientCallbackCallback(manager);
98
99 int fdTimer = timerfd_create(CLOCK_MONOTONIC, 0 /*flags*/);
100 LOG_ALWAYS_FATAL_IF(fdTimer < 0, "Failed to timerfd_create: fd: %d err: %d", fdTimer, errno);
101
102 itimerspec timespec {
103 .it_interval = {
104 .tv_sec = 5,
105 .tv_nsec = 0,
106 },
107 .it_value = {
108 .tv_sec = 5,
109 .tv_nsec = 0,
110 },
111 };
112
113 int timeRes = timerfd_settime(fdTimer, 0 /*flags*/, ×pec, nullptr);
114 LOG_ALWAYS_FATAL_IF(timeRes < 0, "Failed to timerfd_settime: res: %d err: %d", timeRes, errno);
115
116 int addRes = looper->addFd(fdTimer,
117 Looper::POLL_CALLBACK,
118 Looper::EVENT_INPUT,
119 cb,
120 nullptr);
121 LOG_ALWAYS_FATAL_IF(addRes != 1, "Failed to add client callback FD to Looper");
122
123 return cb;
124 }
125
handleEvent(int fd,int,void *)126 int handleEvent(int fd, int /*events*/, void* /*data*/) override {
127 uint64_t expirations;
128 int ret = read(fd, &expirations, sizeof(expirations));
129 if (ret != sizeof(expirations)) {
130 ALOGE("Read failed to callback FD: ret: %d err: %d", ret, errno);
131 }
132
133 mManager->handleClientCallbacks();
134 return 1; // Continue receiving callbacks.
135 }
136 private:
ClientCallbackCallback(const sp<ServiceManager> & manager)137 ClientCallbackCallback(const sp<ServiceManager>& manager) : mManager(manager) {}
138 sp<ServiceManager> mManager;
139 };
140
main()141 int main() {
142 // If hwservicemanager crashes, the system may be unstable and hard to debug. This is both why
143 // we log this and why we care about this at all.
144 setProcessHidlReturnRestriction(HidlReturnRestriction::ERROR_IF_UNCHECKED);
145
146 // TODO(b/36424585): make fatal
147 ProcessState::self()->setCallRestriction(ProcessState::CallRestriction::ERROR_IF_NOT_ONEWAY);
148
149 sp<ServiceManager> manager = new ServiceManager();
150 setRequestingSid(manager, true);
151
152 if (!manager->add(serviceName, manager).withDefault(false)) {
153 ALOGE("Failed to register hwservicemanager with itself.");
154 }
155
156 sp<TokenManager> tokenManager = new TokenManager();
157 if (!manager->add(serviceName, tokenManager).withDefault(false)) {
158 ALOGE("Failed to register ITokenManager with hwservicemanager.");
159 }
160
161 // Tell IPCThreadState we're the service manager
162 sp<IBinder> binder = toBinder<IServiceManager>(manager);
163 sp<BHwBinder> service = static_cast<BHwBinder*>(binder.get()); // local binder object
164 IPCThreadState::self()->setTheContextObject(service);
165 // Then tell the kernel
166 ProcessState::self()->becomeContextManager();
167
168 int rc = property_set("hwservicemanager.ready", "true");
169 if (rc) {
170 ALOGE("Failed to set \"hwservicemanager.ready\" (error %d). "\
171 "HAL services will not start!\n", rc);
172 }
173
174 sp<Looper> looper = Looper::prepare(0 /* opts */);
175
176 (void)HwBinderCallback::setupTo(looper);
177 (void)ClientCallbackCallback::setupTo(looper, manager);
178
179 ALOGI("hwservicemanager is ready now.");
180
181 while (true) {
182 looper->pollAll(-1 /* timeoutMillis */);
183 }
184
185 return 0;
186 }
187