ashmemd.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. #include <android-base/unique_fd.h>
  17. #include <binder/BinderService.h>
  18. #include <binder/IPCThreadState.h>
  19. #include <binder/IServiceManager.h>
  20. #include <binder/ProcessState.h>
  21. #include <binder/Status.h>
  22. #include <utils/String16.h>
  23. #include <android/ashmemd/BnAshmemDeviceService.h>
  24. using android::String16;
  25. using android::base::unique_fd;
  26. namespace android {
  27. namespace ashmemd {
  28. class AshmemDeviceService : public BnAshmemDeviceService {
  29. public:
  30. binder::Status open(os::ParcelFileDescriptor* ashmemFd) override {
  31. ashmemFd->reset(unique_fd(TEMP_FAILURE_RETRY(::open("/dev/ashmem", O_RDWR | O_CLOEXEC))));
  32. return binder::Status::ok();
  33. }
  34. };
  35. void CreateAndRegisterService() {
  36. sp<AshmemDeviceService> ashmemService = new AshmemDeviceService();
  37. defaultServiceManager()->addService(String16("ashmem_device_service"), ashmemService,
  38. true /* allowIsolated */);
  39. }
  40. void JoinThreadPool() {
  41. sp<ProcessState> ps = ProcessState::self();
  42. IPCThreadState::self()->joinThreadPool(); // should not return
  43. }
  44. } // namespace ashmemd
  45. } // namespace android
  46. int main() {
  47. android::ashmemd::CreateAndRegisterService();
  48. android::ashmemd::JoinThreadPool();
  49. std::abort(); // unreachable
  50. }