IShellCallback.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (C) 2016 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. #define LOG_TAG "ShellCallback"
  17. #include <unistd.h>
  18. #include <fcntl.h>
  19. #include <binder/IShellCallback.h>
  20. #include <utils/Log.h>
  21. #include <binder/Parcel.h>
  22. #include <utils/String8.h>
  23. #include <private/binder/Static.h>
  24. namespace android {
  25. // ----------------------------------------------------------------------
  26. class BpShellCallback : public BpInterface<IShellCallback>
  27. {
  28. public:
  29. explicit BpShellCallback(const sp<IBinder>& impl)
  30. : BpInterface<IShellCallback>(impl)
  31. {
  32. }
  33. virtual int openFile(const String16& path, const String16& seLinuxContext,
  34. const String16& mode) {
  35. Parcel data, reply;
  36. data.writeInterfaceToken(IShellCallback::getInterfaceDescriptor());
  37. data.writeString16(path);
  38. data.writeString16(seLinuxContext);
  39. data.writeString16(mode);
  40. remote()->transact(OP_OPEN_OUTPUT_FILE, data, &reply, 0);
  41. reply.readExceptionCode();
  42. int fd = reply.readParcelFileDescriptor();
  43. return fd >= 0 ? fcntl(fd, F_DUPFD_CLOEXEC, 0) : fd;
  44. }
  45. };
  46. IMPLEMENT_META_INTERFACE(ShellCallback, "com.android.internal.os.IShellCallback");
  47. // ----------------------------------------------------------------------
  48. // NOLINTNEXTLINE(google-default-arguments)
  49. status_t BnShellCallback::onTransact(
  50. uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
  51. {
  52. switch(code) {
  53. case OP_OPEN_OUTPUT_FILE: {
  54. CHECK_INTERFACE(IShellCallback, data, reply);
  55. String16 path(data.readString16());
  56. String16 seLinuxContext(data.readString16());
  57. String16 mode(data.readString16());
  58. int fd = openFile(path, seLinuxContext, mode);
  59. if (reply != nullptr) {
  60. reply->writeNoException();
  61. if (fd >= 0) {
  62. reply->writeInt32(1);
  63. reply->writeParcelFileDescriptor(fd, true);
  64. } else {
  65. reply->writeInt32(0);
  66. }
  67. } else if (fd >= 0) {
  68. close(fd);
  69. }
  70. return NO_ERROR;
  71. } break;
  72. default:
  73. return BBinder::onTransact(code, data, reply, flags);
  74. }
  75. }
  76. }; // namespace android