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 <android-base/properties.h>
19 #include <binder/IPCThreadState.h>
20 #include <binder/ProcessState.h>
21 #include <binder/Status.h>
22 #include <sys/timerfd.h>
23 #include <utils/Looper.h>
24 #include <utils/StrongPointer.h>
25
26 #include "Access.h"
27 #include "ServiceManager.h"
28
29 #if !defined(VENDORSERVICEMANAGER) && !defined(__ANDROID_RECOVERY__)
30
31 #include <perfetto/public/producer.h>
32 #include <perfetto/public/te_category_macros.h>
33 #include <perfetto/public/te_macros.h>
34 #include <perfetto/public/track_event.h>
35
36 namespace android {
37
register_perfetto_te_categories()38 static void register_perfetto_te_categories() {
39 struct PerfettoProducerInitArgs perfetto_args = PERFETTO_PRODUCER_INIT_ARGS_INIT();
40 perfetto_args.backends = PERFETTO_BACKEND_SYSTEM;
41 PerfettoProducerInit(perfetto_args);
42 PerfettoTeInit();
43 PERFETTO_TE_REGISTER_CATEGORIES(PERFETTO_SM_CATEGORIES);
44 }
45 } // namespace android
46
47 #endif // !defined(VENDORSERVICEMANAGER) && !defined(__ANDROID_RECOVERY__)
48
49 using ::android::Access;
50 using ::android::IPCThreadState;
51 using ::android::Looper;
52 using ::android::LooperCallback;
53 using ::android::ProcessState;
54 using ::android::ServiceManager;
55 using ::android::sp;
56 using ::android::base::SetProperty;
57 using ::android::os::IServiceManager;
58
59 class BinderCallback : public LooperCallback {
60 public:
setupTo(const sp<Looper> & looper)61 static sp<BinderCallback> setupTo(const sp<Looper>& looper) {
62 sp<BinderCallback> cb = sp<BinderCallback>::make();
63 cb->mLooper = looper;
64
65 IPCThreadState::self()->setupPolling(&cb->mBinderFd);
66 LOG_ALWAYS_FATAL_IF(cb->mBinderFd < 0, "Failed to setupPolling: %d", cb->mBinderFd);
67
68 int ret = looper->addFd(cb->mBinderFd, Looper::POLL_CALLBACK, Looper::EVENT_INPUT, cb,
69 nullptr /*data*/);
70 LOG_ALWAYS_FATAL_IF(ret != 1, "Failed to add binder FD to Looper");
71
72 return cb;
73 }
74
handleEvent(int,int,void *)75 int handleEvent(int /* fd */, int /* events */, void* /* data */) override {
76 IPCThreadState::self()->handlePolledCommands();
77 return 1; // Continue receiving callbacks.
78 }
79
repoll()80 void repoll() {
81 if (!mLooper->repoll(mBinderFd)) {
82 ALOGE("Failed to repoll binder FD.");
83 }
84 }
85
86 private:
87 sp<Looper> mLooper;
88 int mBinderFd = -1;
89 };
90
91 // LooperCallback for IClientCallback
92 class ClientCallbackCallback : public LooperCallback {
93 public:
setupTo(const sp<Looper> & looper,const sp<ServiceManager> & manager,sp<BinderCallback> binderCallback)94 static sp<ClientCallbackCallback> setupTo(const sp<Looper>& looper,
95 const sp<ServiceManager>& manager,
96 sp<BinderCallback> binderCallback) {
97 sp<ClientCallbackCallback> cb = sp<ClientCallbackCallback>::make(manager);
98 cb->mBinderCallback = binderCallback;
99
100 int fdTimer = timerfd_create(CLOCK_MONOTONIC, 0 /*flags*/);
101 LOG_ALWAYS_FATAL_IF(fdTimer < 0, "Failed to timerfd_create: fd: %d err: %d", fdTimer, errno);
102
103 itimerspec timespec {
104 .it_interval = {
105 .tv_sec = 5,
106 .tv_nsec = 0,
107 },
108 .it_value = {
109 .tv_sec = 5,
110 .tv_nsec = 0,
111 },
112 };
113
114 int timeRes = timerfd_settime(fdTimer, 0 /*flags*/, ×pec, nullptr);
115 LOG_ALWAYS_FATAL_IF(timeRes < 0, "Failed to timerfd_settime: res: %d err: %d", timeRes, errno);
116
117 int addRes = looper->addFd(fdTimer,
118 Looper::POLL_CALLBACK,
119 Looper::EVENT_INPUT,
120 cb,
121 nullptr);
122 LOG_ALWAYS_FATAL_IF(addRes != 1, "Failed to add client callback FD to Looper");
123
124 return cb;
125 }
126
handleEvent(int fd,int,void *)127 int handleEvent(int fd, int /*events*/, void* /*data*/) override {
128 uint64_t expirations;
129 int ret = read(fd, &expirations, sizeof(expirations));
130 if (ret != sizeof(expirations)) {
131 ALOGE("Read failed to callback FD: ret: %d err: %d", ret, errno);
132 }
133
134 mManager->handleClientCallbacks();
135 mBinderCallback->repoll(); // b/316829336
136
137 return 1; // Continue receiving callbacks.
138 }
139 private:
140 friend sp<ClientCallbackCallback>;
ClientCallbackCallback(const sp<ServiceManager> & manager)141 ClientCallbackCallback(const sp<ServiceManager>& manager) : mManager(manager) {}
142 sp<ServiceManager> mManager;
143 sp<BinderCallback> mBinderCallback;
144 };
145
main(int argc,char ** argv)146 int main(int argc, char** argv) {
147 android::base::InitLogging(argv, android::base::KernelLogger);
148
149 if (argc > 2) {
150 LOG(FATAL) << "usage: " << argv[0] << " [binder driver]";
151 }
152
153 const char* driver = argc == 2 ? argv[1] : "/dev/binder";
154
155 #if !defined(VENDORSERVICEMANAGER) && !defined(__ANDROID_RECOVERY__)
156 android::register_perfetto_te_categories();
157 #endif // !defined(VENDORSERVICEMANAGER) && !defined(__ANDROID_RECOVERY__)
158
159 LOG(INFO) << "Starting sm instance on " << driver;
160
161 sp<ProcessState> ps = ProcessState::initWithDriver(driver);
162 ps->setThreadPoolMaxThreadCount(0);
163 ps->setCallRestriction(ProcessState::CallRestriction::FATAL_IF_NOT_ONEWAY);
164
165 IPCThreadState::self()->disableBackgroundScheduling(true);
166
167 sp<ServiceManager> manager = sp<ServiceManager>::make(std::make_unique<Access>());
168 manager->setRequestingSid(true);
169 if (!manager->addService("manager", manager, false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()) {
170 LOG(ERROR) << "Could not self register servicemanager";
171 }
172
173 IPCThreadState::self()->setTheContextObject(manager);
174 if (!ps->becomeContextManager()) {
175 LOG(FATAL) << "Could not become context manager";
176 }
177
178 sp<Looper> looper = Looper::prepare(false /*allowNonCallbacks*/);
179
180 sp<BinderCallback> binderCallback = BinderCallback::setupTo(looper);
181 ClientCallbackCallback::setupTo(looper, manager, binderCallback);
182
183 #ifndef VENDORSERVICEMANAGER
184 if (!SetProperty("servicemanager.ready", "true")) {
185 LOG(ERROR) << "Failed to set servicemanager ready property";
186 }
187 #endif
188
189 while(true) {
190 looper->pollAll(-1);
191 }
192
193 // should not be reached
194 return EXIT_FAILURE;
195 }
196