Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 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> |
Steven Moreland | 454bfd9 | 2022-07-20 20:53:36 +0000 | [diff] [blame] | 18 | #include <android-base/properties.h> |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 19 | #include <binder/IPCThreadState.h> |
| 20 | #include <binder/ProcessState.h> |
| 21 | #include <binder/Status.h> |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 22 | #include <sys/timerfd.h> |
| 23 | #include <utils/Looper.h> |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 24 | #include <utils/StrongPointer.h> |
| 25 | |
| 26 | #include "Access.h" |
| 27 | #include "ServiceManager.h" |
| 28 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 29 | using ::android::Access; |
Steven Moreland | 454bfd9 | 2022-07-20 20:53:36 +0000 | [diff] [blame] | 30 | using ::android::IPCThreadState; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 31 | using ::android::Looper; |
| 32 | using ::android::LooperCallback; |
| 33 | using ::android::ProcessState; |
Steven Moreland | 46f380b | 2019-10-16 16:28:21 -0700 | [diff] [blame] | 34 | using ::android::ServiceManager; |
Steven Moreland | 46f380b | 2019-10-16 16:28:21 -0700 | [diff] [blame] | 35 | using ::android::sp; |
Steven Moreland | 454bfd9 | 2022-07-20 20:53:36 +0000 | [diff] [blame] | 36 | using ::android::base::SetProperty; |
| 37 | using ::android::os::IServiceManager; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 38 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 39 | class BinderCallback : public LooperCallback { |
| 40 | public: |
| 41 | static sp<BinderCallback> setupTo(const sp<Looper>& looper) { |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 42 | sp<BinderCallback> cb = sp<BinderCallback>::make(); |
Steven Moreland | 535f8f9 | 2024-01-20 01:55:56 +0000 | [diff] [blame] | 43 | cb->mLooper = looper; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 44 | |
Steven Moreland | 535f8f9 | 2024-01-20 01:55:56 +0000 | [diff] [blame] | 45 | IPCThreadState::self()->setupPolling(&cb->mBinderFd); |
| 46 | LOG_ALWAYS_FATAL_IF(cb->mBinderFd < 0, "Failed to setupPolling: %d", cb->mBinderFd); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 47 | |
Steven Moreland | 535f8f9 | 2024-01-20 01:55:56 +0000 | [diff] [blame] | 48 | int ret = looper->addFd(cb->mBinderFd, Looper::POLL_CALLBACK, Looper::EVENT_INPUT, cb, |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 49 | nullptr /*data*/); |
| 50 | LOG_ALWAYS_FATAL_IF(ret != 1, "Failed to add binder FD to Looper"); |
| 51 | |
| 52 | return cb; |
| 53 | } |
| 54 | |
| 55 | int handleEvent(int /* fd */, int /* events */, void* /* data */) override { |
| 56 | IPCThreadState::self()->handlePolledCommands(); |
| 57 | return 1; // Continue receiving callbacks. |
| 58 | } |
Steven Moreland | 535f8f9 | 2024-01-20 01:55:56 +0000 | [diff] [blame] | 59 | |
| 60 | void repoll() { |
| 61 | if (!mLooper->repoll(mBinderFd)) { |
| 62 | ALOGE("Failed to repoll binder FD."); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | sp<Looper> mLooper; |
| 68 | int mBinderFd = -1; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | // LooperCallback for IClientCallback |
| 72 | class ClientCallbackCallback : public LooperCallback { |
| 73 | public: |
Steven Moreland | 535f8f9 | 2024-01-20 01:55:56 +0000 | [diff] [blame] | 74 | static sp<ClientCallbackCallback> setupTo(const sp<Looper>& looper, |
| 75 | const sp<ServiceManager>& manager, |
| 76 | sp<BinderCallback> binderCallback) { |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 77 | sp<ClientCallbackCallback> cb = sp<ClientCallbackCallback>::make(manager); |
Steven Moreland | 535f8f9 | 2024-01-20 01:55:56 +0000 | [diff] [blame] | 78 | cb->mBinderCallback = binderCallback; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 79 | |
| 80 | int fdTimer = timerfd_create(CLOCK_MONOTONIC, 0 /*flags*/); |
| 81 | LOG_ALWAYS_FATAL_IF(fdTimer < 0, "Failed to timerfd_create: fd: %d err: %d", fdTimer, errno); |
| 82 | |
| 83 | itimerspec timespec { |
| 84 | .it_interval = { |
| 85 | .tv_sec = 5, |
| 86 | .tv_nsec = 0, |
| 87 | }, |
| 88 | .it_value = { |
| 89 | .tv_sec = 5, |
| 90 | .tv_nsec = 0, |
| 91 | }, |
| 92 | }; |
| 93 | |
| 94 | int timeRes = timerfd_settime(fdTimer, 0 /*flags*/, ×pec, nullptr); |
| 95 | LOG_ALWAYS_FATAL_IF(timeRes < 0, "Failed to timerfd_settime: res: %d err: %d", timeRes, errno); |
| 96 | |
| 97 | int addRes = looper->addFd(fdTimer, |
| 98 | Looper::POLL_CALLBACK, |
| 99 | Looper::EVENT_INPUT, |
| 100 | cb, |
| 101 | nullptr); |
| 102 | LOG_ALWAYS_FATAL_IF(addRes != 1, "Failed to add client callback FD to Looper"); |
| 103 | |
| 104 | return cb; |
| 105 | } |
| 106 | |
| 107 | int handleEvent(int fd, int /*events*/, void* /*data*/) override { |
| 108 | uint64_t expirations; |
| 109 | int ret = read(fd, &expirations, sizeof(expirations)); |
| 110 | if (ret != sizeof(expirations)) { |
| 111 | ALOGE("Read failed to callback FD: ret: %d err: %d", ret, errno); |
| 112 | } |
| 113 | |
| 114 | mManager->handleClientCallbacks(); |
Steven Moreland | 535f8f9 | 2024-01-20 01:55:56 +0000 | [diff] [blame] | 115 | mBinderCallback->repoll(); // b/316829336 |
| 116 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 117 | return 1; // Continue receiving callbacks. |
| 118 | } |
| 119 | private: |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 120 | friend sp<ClientCallbackCallback>; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 121 | ClientCallbackCallback(const sp<ServiceManager>& manager) : mManager(manager) {} |
| 122 | sp<ServiceManager> mManager; |
Steven Moreland | 535f8f9 | 2024-01-20 01:55:56 +0000 | [diff] [blame] | 123 | sp<BinderCallback> mBinderCallback; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 124 | }; |
| 125 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 126 | int main(int argc, char** argv) { |
Yifan Hong | 278d81f | 2021-11-12 19:52:35 -0800 | [diff] [blame] | 127 | android::base::InitLogging(argv, android::base::KernelLogger); |
Yifan Hong | 278d81f | 2021-11-12 19:52:35 -0800 | [diff] [blame] | 128 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 129 | if (argc > 2) { |
| 130 | LOG(FATAL) << "usage: " << argv[0] << " [binder driver]"; |
| 131 | } |
| 132 | |
| 133 | const char* driver = argc == 2 ? argv[1] : "/dev/binder"; |
| 134 | |
Steven Moreland | 86cdb08 | 2022-07-06 22:30:18 +0000 | [diff] [blame] | 135 | LOG(INFO) << "Starting sm instance on " << driver; |
| 136 | |
Steven Moreland | 1ac84c4 | 2019-07-10 17:51:27 -0700 | [diff] [blame] | 137 | sp<ProcessState> ps = ProcessState::initWithDriver(driver); |
| 138 | ps->setThreadPoolMaxThreadCount(0); |
| 139 | ps->setCallRestriction(ProcessState::CallRestriction::FATAL_IF_NOT_ONEWAY); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 140 | |
Steven Moreland | 310fce2 | 2023-08-30 20:57:11 +0000 | [diff] [blame] | 141 | IPCThreadState::self()->disableBackgroundScheduling(true); |
| 142 | |
Steven Moreland | b098318 | 2021-04-02 03:14:04 +0000 | [diff] [blame] | 143 | sp<ServiceManager> manager = sp<ServiceManager>::make(std::make_unique<Access>()); |
Steven Moreland | 46f380b | 2019-10-16 16:28:21 -0700 | [diff] [blame] | 144 | if (!manager->addService("manager", manager, false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()) { |
| 145 | LOG(ERROR) << "Could not self register servicemanager"; |
| 146 | } |
| 147 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 148 | IPCThreadState::self()->setTheContextObject(manager); |
Steven Moreland | d98952d | 2023-05-11 23:27:43 +0000 | [diff] [blame] | 149 | if (!ps->becomeContextManager()) { |
Steven Moreland | 601b795 | 2023-05-11 23:29:00 +0000 | [diff] [blame] | 150 | LOG(FATAL) << "Could not become context manager"; |
Steven Moreland | d98952d | 2023-05-11 23:27:43 +0000 | [diff] [blame] | 151 | } |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 152 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 153 | sp<Looper> looper = Looper::prepare(false /*allowNonCallbacks*/); |
| 154 | |
Steven Moreland | 535f8f9 | 2024-01-20 01:55:56 +0000 | [diff] [blame] | 155 | sp<BinderCallback> binderCallback = BinderCallback::setupTo(looper); |
| 156 | ClientCallbackCallback::setupTo(looper, manager, binderCallback); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 157 | |
Steven Moreland | 454bfd9 | 2022-07-20 20:53:36 +0000 | [diff] [blame] | 158 | #ifndef VENDORSERVICEMANAGER |
| 159 | if (!SetProperty("servicemanager.ready", "true")) { |
| 160 | LOG(ERROR) << "Failed to set servicemanager ready property"; |
| 161 | } |
| 162 | #endif |
| 163 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 164 | while(true) { |
| 165 | looper->pollAll(-1); |
| 166 | } |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 167 | |
| 168 | // should not be reached |
| 169 | return EXIT_FAILURE; |
| 170 | } |