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/ServiceManagement.h>
30 #include <hidl/Status.h>
31 #include <hwbinder/IPCThreadState.h>
32 #include <hwbinder/ProcessState.h>
33 #include <utils/Errors.h>
34 #include <utils/Looper.h>
35 #include <utils/StrongPointer.h>
36 
37 #include "ServiceManager.h"
38 #include "TokenManager.h"
39 #include "Vintf.h"
40 
41 // libutils:
42 using android::sp;
43 using android::Looper;
44 using android::LooperCallback;
45 
46 // libhwbinder:
47 using android::hardware::BHwBinder;
48 using android::hardware::IBinder;
49 using android::hardware::IPCThreadState;
50 using android::hardware::ProcessState;
51 
52 // libhidl
53 using android::hardware::handleTransportPoll;
54 using android::hardware::setRequestingSid;
55 using android::hardware::HidlReturnRestriction;
56 using android::hardware::setProcessHidlReturnRestriction;
57 using android::hardware::setupTransportPolling;
58 using android::hardware::toBinder;
59 
60 // implementations
61 using android::hidl::manager::implementation::ServiceManager;
62 using android::hidl::manager::V1_0::IServiceManager;
63 using android::hidl::token::V1_0::implementation::TokenManager;
64 
65 static std::string serviceName = "default";
66 
67 class HwBinderCallback : public LooperCallback {
68 public:
setupTo(const sp<Looper> & looper)69     static sp<HwBinderCallback> setupTo(const sp<Looper>& looper) {
70         sp<HwBinderCallback> cb = new HwBinderCallback;
71 
72         int fdHwBinder = setupTransportPolling();
73         LOG_ALWAYS_FATAL_IF(fdHwBinder < 0, "Failed to setupTransportPolling: %d", fdHwBinder);
74 
75         // Flush after setupPolling(), to make sure the binder driver
76         // knows about this thread handling commands.
77         IPCThreadState::self()->flushCommands();
78 
79         int ret = looper->addFd(fdHwBinder,
80                                 Looper::POLL_CALLBACK,
81                                 Looper::EVENT_INPUT,
82                                 cb,
83                                 nullptr /*data*/);
84         LOG_ALWAYS_FATAL_IF(ret != 1, "Failed to add binder FD to Looper");
85 
86         return cb;
87     }
88 
handleEvent(int fd,int,void *)89     int handleEvent(int fd, int /*events*/, void* /*data*/) override {
90         handleTransportPoll(fd);
91         return 1;  // Continue receiving callbacks.
92     }
93 };
94 
95 // LooperCallback for IClientCallback
96 class ClientCallbackCallback : public LooperCallback {
97 public:
setupTo(const sp<Looper> & looper,const sp<ServiceManager> & manager)98     static sp<ClientCallbackCallback> setupTo(const sp<Looper>& looper, const sp<ServiceManager>& manager) {
99         sp<ClientCallbackCallback> cb = new ClientCallbackCallback(manager);
100 
101         int fdTimer = timerfd_create(CLOCK_MONOTONIC, 0 /*flags*/);
102         LOG_ALWAYS_FATAL_IF(fdTimer < 0, "Failed to timerfd_create: fd: %d err: %d", fdTimer, errno);
103 
104         itimerspec timespec {
105             .it_interval = {
106                 .tv_sec = 5,
107                 .tv_nsec = 0,
108             },
109             .it_value = {
110                 .tv_sec = 5,
111                 .tv_nsec = 0,
112             },
113         };
114 
115         int timeRes = timerfd_settime(fdTimer, 0 /*flags*/, &timespec, nullptr);
116         LOG_ALWAYS_FATAL_IF(timeRes < 0, "Failed to timerfd_settime: res: %d err: %d", timeRes, errno);
117 
118         int addRes = looper->addFd(fdTimer,
119                                    Looper::POLL_CALLBACK,
120                                    Looper::EVENT_INPUT,
121                                    cb,
122                                    nullptr);
123         LOG_ALWAYS_FATAL_IF(addRes != 1, "Failed to add client callback FD to Looper");
124 
125         return cb;
126     }
127 
handleEvent(int fd,int,void *)128     int handleEvent(int fd, int /*events*/, void* /*data*/) override {
129         uint64_t expirations;
130         int ret = read(fd, &expirations, sizeof(expirations));
131         if (ret != sizeof(expirations)) {
132             ALOGE("Read failed to callback FD: ret: %d err: %d", ret, errno);
133         }
134 
135         mManager->handleClientCallbacks();
136         return 1;  // Continue receiving callbacks.
137     }
138 private:
ClientCallbackCallback(const sp<ServiceManager> & manager)139     ClientCallbackCallback(const sp<ServiceManager>& manager) : mManager(manager) {}
140     sp<ServiceManager> mManager;
141 };
142 
main()143 int main() {
144     // If hwservicemanager crashes, the system may be unstable and hard to debug. This is both why
145     // we log this and why we care about this at all.
146     setProcessHidlReturnRestriction(HidlReturnRestriction::ERROR_IF_UNCHECKED);
147 
148     // TODO(b/36424585): make fatal
149     ProcessState::self()->setCallRestriction(ProcessState::CallRestriction::ERROR_IF_NOT_ONEWAY);
150     auto transport = android::hardware::getTransport(ServiceManager::descriptor, serviceName);
151     if (transport == android::vintf::Transport::EMPTY) {
152         ALOGI("HIDL is not supported on this device so hwservicemanager is not needed");
153         int rc = property_set("hwservicemanager.disabled", "true");
154         if (rc) {
155             LOG_ALWAYS_FATAL("Failed to set \"hwservicemanager.disabled\" (error %d).\"", rc);
156         }
157         // wait here for init to see the proprty and shut us down
158         while (true) {
159             ALOGW("Waiting on init to shut this process down.");
160             sleep(10);
161         }
162     }
163 
164     sp<ServiceManager> manager = new ServiceManager();
165     setRequestingSid(manager, true);
166 
167     if (!manager->add(serviceName, manager).withDefault(false)) {
168         ALOGE("Failed to register hwservicemanager with itself.");
169     }
170 
171     // Check to make sure we should be registering tokenManager first. Only if
172     // it's declared in the manifest.
173     sp<TokenManager> tokenManager;
174     if (android::vintf::Transport::EMPTY !=
175         android::hardware::getTransport(TokenManager::descriptor, serviceName)) {
176         tokenManager = new TokenManager();
177         if (!manager->add(serviceName, tokenManager).withDefault(false)) {
178             ALOGE("Failed to register ITokenManager with hwservicemanager.");
179         }
180     } else {
181         ALOGW("Not registering android.hidl.token service because it is no longer supported");
182     }
183 
184     // Tell IPCThreadState we're the service manager
185     sp<IBinder> binder = toBinder<IServiceManager>(manager);
186     sp<BHwBinder> service = static_cast<BHwBinder*>(binder.get()); // local binder object
187     IPCThreadState::self()->setTheContextObject(service);
188     // Then tell the kernel
189     ProcessState::self()->becomeContextManager();
190 
191     int rc = property_set("hwservicemanager.ready", "true");
192     if (rc) {
193         ALOGE("Failed to set \"hwservicemanager.ready\" (error %d). "\
194               "HAL services will not start!\n", rc);
195     }
196 
197     sp<Looper> looper = Looper::prepare(0 /* opts */);
198 
199     (void)HwBinderCallback::setupTo(looper);
200     (void)ClientCallbackCallback::setupTo(looper, manager);
201 
202     ALOGI("hwservicemanager is ready now.");
203 
204     while (true) {
205         looper->pollAll(-1 /* timeoutMillis */);
206     }
207 
208     return 0;
209 }
210