1 /*
2 * Copyright (C) 2019 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 <android-base/logging.h>
18 #include <binder/IPCThreadState.h>
19 #include <binder/ProcessState.h>
20 #include <binder/Status.h>
21 #include <sys/timerfd.h>
22 #include <utils/Looper.h>
23 #include <utils/StrongPointer.h>
24
25 #include "Access.h"
26 #include "ServiceManager.h"
27
28 using ::android::Access;
29 using ::android::sp;
30 using ::android::Looper;
31 using ::android::LooperCallback;
32 using ::android::ProcessState;
33 using ::android::IPCThreadState;
34 using ::android::ProcessState;
35 using ::android::ServiceManager;
36 using ::android::os::IServiceManager;
37 using ::android::sp;
38
39 class BinderCallback : public LooperCallback {
40 public:
setupTo(const sp<Looper> & looper)41 static sp<BinderCallback> setupTo(const sp<Looper>& looper) {
42 sp<BinderCallback> cb = new BinderCallback;
43
44 int binder_fd = -1;
45 IPCThreadState::self()->setupPolling(&binder_fd);
46 LOG_ALWAYS_FATAL_IF(binder_fd < 0, "Failed to setupPolling: %d", binder_fd);
47
48 // Flush after setupPolling(), to make sure the binder driver
49 // knows about this thread handling commands.
50 IPCThreadState::self()->flushCommands();
51
52 int ret = looper->addFd(binder_fd,
53 Looper::POLL_CALLBACK,
54 Looper::EVENT_INPUT,
55 cb,
56 nullptr /*data*/);
57 LOG_ALWAYS_FATAL_IF(ret != 1, "Failed to add binder FD to Looper");
58
59 return cb;
60 }
61
handleEvent(int,int,void *)62 int handleEvent(int /* fd */, int /* events */, void* /* data */) override {
63 IPCThreadState::self()->handlePolledCommands();
64 return 1; // Continue receiving callbacks.
65 }
66 };
67
68 // LooperCallback for IClientCallback
69 class ClientCallbackCallback : public LooperCallback {
70 public:
setupTo(const sp<Looper> & looper,const sp<ServiceManager> & manager)71 static sp<ClientCallbackCallback> setupTo(const sp<Looper>& looper, const sp<ServiceManager>& manager) {
72 sp<ClientCallbackCallback> cb = new ClientCallbackCallback(manager);
73
74 int fdTimer = timerfd_create(CLOCK_MONOTONIC, 0 /*flags*/);
75 LOG_ALWAYS_FATAL_IF(fdTimer < 0, "Failed to timerfd_create: fd: %d err: %d", fdTimer, errno);
76
77 itimerspec timespec {
78 .it_interval = {
79 .tv_sec = 5,
80 .tv_nsec = 0,
81 },
82 .it_value = {
83 .tv_sec = 5,
84 .tv_nsec = 0,
85 },
86 };
87
88 int timeRes = timerfd_settime(fdTimer, 0 /*flags*/, ×pec, nullptr);
89 LOG_ALWAYS_FATAL_IF(timeRes < 0, "Failed to timerfd_settime: res: %d err: %d", timeRes, errno);
90
91 int addRes = looper->addFd(fdTimer,
92 Looper::POLL_CALLBACK,
93 Looper::EVENT_INPUT,
94 cb,
95 nullptr);
96 LOG_ALWAYS_FATAL_IF(addRes != 1, "Failed to add client callback FD to Looper");
97
98 return cb;
99 }
100
handleEvent(int fd,int,void *)101 int handleEvent(int fd, int /*events*/, void* /*data*/) override {
102 uint64_t expirations;
103 int ret = read(fd, &expirations, sizeof(expirations));
104 if (ret != sizeof(expirations)) {
105 ALOGE("Read failed to callback FD: ret: %d err: %d", ret, errno);
106 }
107
108 mManager->handleClientCallbacks();
109 return 1; // Continue receiving callbacks.
110 }
111 private:
ClientCallbackCallback(const sp<ServiceManager> & manager)112 ClientCallbackCallback(const sp<ServiceManager>& manager) : mManager(manager) {}
113 sp<ServiceManager> mManager;
114 };
115
main(int argc,char ** argv)116 int main(int argc, char** argv) {
117 if (argc > 2) {
118 LOG(FATAL) << "usage: " << argv[0] << " [binder driver]";
119 }
120
121 const char* driver = argc == 2 ? argv[1] : "/dev/binder";
122
123 sp<ProcessState> ps = ProcessState::initWithDriver(driver);
124 ps->setThreadPoolMaxThreadCount(0);
125 ps->setCallRestriction(ProcessState::CallRestriction::FATAL_IF_NOT_ONEWAY);
126
127 sp<ServiceManager> manager = new ServiceManager(std::make_unique<Access>());
128 if (!manager->addService("manager", manager, false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()) {
129 LOG(ERROR) << "Could not self register servicemanager";
130 }
131
132 IPCThreadState::self()->setTheContextObject(manager);
133 ps->becomeContextManager(nullptr, nullptr);
134
135 sp<Looper> looper = Looper::prepare(false /*allowNonCallbacks*/);
136
137 BinderCallback::setupTo(looper);
138 ClientCallbackCallback::setupTo(looper, manager);
139
140 while(true) {
141 looper->pollAll(-1);
142 }
143
144 // should not be reached
145 return EXIT_FAILURE;
146 }
147