Christopher Wiley | 9e1eda9 | 2015-11-16 15:23:37 -0800 | [diff] [blame^] | 1 | // |
| 2 | // Copyright (C) 2015 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 <binder/IPCThreadState.h> |
| 18 | #include <binder/IServiceManager.h> |
| 19 | #include <binder/ProcessState.h> |
| 20 | #include <utils/Errors.h> |
| 21 | #include <utils/Log.h> |
| 22 | #include <utils/Looper.h> |
| 23 | #include <utils/StrongPointer.h> |
| 24 | |
| 25 | #include "update_engine/binder_service.h" |
| 26 | |
| 27 | // Log to logcat as update_engine. |
| 28 | #undef LOG_TAG |
| 29 | #define LOG_TAG "update_engine" |
| 30 | |
| 31 | namespace android { |
| 32 | namespace { |
| 33 | |
| 34 | class BinderCallback : public LooperCallback { |
| 35 | public: |
| 36 | BinderCallback() {} |
| 37 | ~BinderCallback() override {} |
| 38 | |
| 39 | int handleEvent(int /* fd */, int /* events */, void* /* data */) override { |
| 40 | IPCThreadState::self()->handlePolledCommands(); |
| 41 | return 1; // Continue receiving callbacks. |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | bool run(const sp<IBinder>& service) { |
| 46 | sp<Looper> looper(Looper::prepare(0 /* opts */)); |
| 47 | |
| 48 | ALOGD("Connecting to binder driver"); |
| 49 | int binder_fd = -1; |
| 50 | ProcessState::self()->setThreadPoolMaxThreadCount(0); |
| 51 | IPCThreadState::self()->disableBackgroundScheduling(true); |
| 52 | IPCThreadState::self()->setupPolling(&binder_fd); |
| 53 | if (binder_fd < 0) { |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | sp<BinderCallback> cb(new BinderCallback); |
| 58 | if (looper->addFd(binder_fd, Looper::POLL_CALLBACK, Looper::EVENT_INPUT, cb, |
| 59 | nullptr) != 1) { |
| 60 | ALOGE("Failed to add binder FD to Looper"); |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | ALOGD("Registering update_engine with the service manager"); |
| 65 | status_t status = defaultServiceManager()->addService( |
| 66 | service->getInterfaceDescriptor(), service); |
| 67 | if (status != android::OK) { |
| 68 | ALOGE("Failed to register update_engine with the service manager."); |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | ALOGD("Entering update_engine mainloop"); |
| 73 | while (true) { |
| 74 | const int result = looper->pollAll(-1 /* timeoutMillis */); |
| 75 | ALOGD("Looper returned %d", result); |
| 76 | } |
| 77 | // We should never get here. |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | } // namespace |
| 82 | } // namespace android |
| 83 | |
| 84 | int main(int argc, char** argv) { |
| 85 | android::sp<android::IBinder> service( |
| 86 | new chromeos_update_engine::BinderService); |
| 87 | if (!android::run(service)) { |
| 88 | return 1; |
| 89 | } |
| 90 | return 0; |
| 91 | } |